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)