Design idea: The process of encrypting is to move the letter in the alphabet two bits backwards, i.e. A programming D, string +3, decrypted string -3 ;
Program Flowchart:
Source:
Wang Zhewen Caesar problem encryption and decryption key: Encryption is a string +3, decryption time-3
Package string11;
Import Java.util.Scanner;
public class Jiami
{
public static void Main (string[] args) throws Exception
{
TODO auto-generated Method Stub
System.out.println ("[A encryption] [J decryption],please Choose one");
Scanner s=new Scanner (system.in);//Create Scanner Object
String S1=s.nextline ();//Get the bank string
if (S1.equalsignorecase ("a"))//Determine the size of S1 and a
{
int key;
System.out.println ("Please enter clear text:");
Scanner sc=new Scanner (system.in);
String Ss=sc.nextline ();
System.out.println ("Please enter key:");
Scanner scc=new Scanner (system.in);
Key=scc.nextint ();//convert input characters to int type
Encryption (Ss,key);//Call encryption method
}
else if (s1.equalsignorecase ("J"));
{
int key;
System.out.println ("Please enter ciphertext:");
Scanner sc=new Scanner (system.in);
String SS =sc.nextline ();
System.out.println ("Please enter key:");
Scanner scc=new Scanner (system.in);
Key=scc.nextint ();
Decrypt (Ss,key);//Call encryption method
}
}
Encryption Program
public static void encryption (String str,int t)
{
String string= "";
int i;
char c;
For (I=1;i<str.length (); i++)
{
C=str.charat (i);
if (c>= ' a ' &&c<= ' z ')//If a character in a character is lowercase
{
C+=t% 26;///Move 26 bit
if (c< ' a ')
c+=26;//to the Zou world
if (c> ' z ')
c-=26;//Right over bounds
}
else if (c>= ' a ' &&c<= ' Z ')//If one of the characters in the character is uppercase
{
C+=t% 26;
if (c< ' A ')
c+=26;//to the Zou world
if (c> ' Z ')
c-=26;//Right over bounds
}
String +=c;//the encrypted character into a string
}
SYSTEM.OUT.PRINTLN (str + "after encryption:" + string);
}
public static void Decrypt (String str,int N)
{
int t;
T=integer.parseint ("-" +n);
String string= "";
int i;
For (I=0;i<str.length (); i++)
{
Char C=str.charat (i);
if (c>= ' a ' &&c<= ' z ')//If a character in a character is lowercase
{
C+=t% 26;//Move 26 bit
if (c< ' a ')
c+=26;//to the Zou world
if (c> ' z ')
c-=26;//Right over bounds
}
else if (c>= ' a ' &&c<= ' Z ')//If one of the characters in the character is uppercase
{
C+=t% 26;
if (c< ' A ')
c+=26;//to the Zou world
if (c> ' Z ')
c-=26;//Right over bounds
}
String +=c;//the encrypted character into a string
}
SYSTEM.OUT.PRINTLN (str + "after decryption:" + string);
}
}
:
Caesar Encryption Problem Program