Caesar Encryption Decryption (Java letter shift)

Source: Internet
Author: User

1. Design idea: Encryption is the conversion of character data into ASC code table of numbers, between a-w by adding 3 and then converted to character output, between x-z by conversion to the ASC Code table in the number minus 23 and then converted to character output. Decryption is the conversion of character data to the number in the ASC code table, between the d-z by subtracting 3 and then converting to the character output, a-c between the conversion to the ASC Code table after the number of 23 and then converted to character output.

2. Program Flowchart

3. Program source code

Import Java.util.Scanner;
public class Code {
Yanyu Qingkai Encryption and decryption 2015 10 23
public static void Main (string[] args) {
TODO auto-generated Method Stub
String input = null;
Scanner sc = new Scanner (system.in);
System.out.println ("Please enter the letter:");
input = Sc.next ();
StringBuffer code = new StringBuffer ();
Scanner sc1=new Scanner (system.in);
SYSTEM.OUT.PRINTLN ("Encrypt please press 1, decrypt please press 2:");
int p;
P=sc1.nextint ();
if (p==1)
{
SYSTEM.OUT.PRINTLN ("After encryption:");
for (int i = 0;i < Input.length (); i++)
{
char x = Input.charat (i);

if (x >= ' a ' && x <= ' W ')
{
x = (char) (X+3);
Code.append (x);
}
if (x >= ' x ' && x <= ' z ')
{
X= (char) (X-23);
Code.append (x);
}

if (x >= ' A ' && x <= ' W ')
{
x = (char) (X+3);

Code.append (x);
}
if (x >= ' x ' && x <= ' Z ')
{
X= (char) (X-23);
Code.append (x);
}

}
}
if (p==2)
{
System.out.println ("decryption:");
for (int i = 0;i < Input.length (); i++)
{
char x = Input.charat (i);
if (x >= ' a ' && x <= ' C ')
{
X= (char) (x+23);
Code.append (x);
}
else if (x >= ' d ' && x <= ' z ')
{
x = (char) (x-3);
Code.append (x);
}
else if (x >= ' A ' && x <= ' C ')
{
X= (char) (x+23);
Code.append (x);
}

else if (x >= ' D ' && x <= ' Z ')
{
x = (char) (x-3);

Code.append (x);
}

}
}
SYSTEM.OUT.PRINTLN (code);

}

}

4. Results

Caesar Encryption Decryption (Java letter shift)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.