One. String encryption
1. Design Ideas
Enter the English string, encrypt or decrypt the English string according to the encryption or decryption password, and finally output the encrypted or decrypted string
2. Program Flowchart
3. Source code
Package Kaisa;
Import Java.util.Scanner;
public class Kaisa
{
public static void Main (string[] args)
{
SYSTEM.OUT.PRINTLN ("Please select: 1. Encrypt 2. Decrypt");
Scanner c=new Scanner (system.in);
String Ss=c.nextline ();
if (Ss.equalsignorecase ("1"))//if statement to determine whether to encrypt or decrypt
{
System.out.println ("Please enter an English string to encrypt");
Scanner sc=new Scanner (system.in);
String S=sc.nextline ();
System.out.println ("Enter encryption password");
Scanner sc1=new Scanner (system.in);
int Key=sc1.nextint ();
Jiami (S,key);
}
else if (Ss.equalsignorecase ("2"));
{
System.out.println ("Please enter the English string to decrypt");
Scanner sc=new Scanner (system.in);
String S=sc.nextline ();
System.out.println ("Please enter the decryption password");
Scanner sc1=new Scanner (system.in);
int Key=sc1.nextint ();
Jiemi (S,key);
}
}
public static void Jiami (String str,int k)//cryptographic function
{
String string= "";
for (int i=0;i<str.length (); i++)
{
Char C=str.charat (i);
if (c>= ' a ' &&c<= ' Z ')
{
c+=k%26;
if (c< ' a ')
c+=26;
if (c> ' z ')
c-=26;
}
else if (c>= ' A ' &&c<= ' Z ')
{
c+=k%26;
if (c< ' A ')
c+=26;
if (c> ' Z ')
c-=26;
}
String+=c;
}
System.out.println (str+ "after encryption:" +string);
}
public static void Jiemi (String str,int N)//decryption function
{
int K=integer.parseint ("-" +n);
String string= "";
for (int i=0;i<str.length (); i++)
{
Char C=str.charat (i);
if (c>= ' a ' &&c<= ' Z ')
{
c+=k%26;
if (c< ' a ')
{
c+=26;
}
if (c> ' z ')
{
c-=26;
}
else if (c>= ' A ' &&c<= ' Z ')
{
c+=k%26;
if (c< ' A ')
{
c+=26;
}
if (c> ' Z ')
{
c-=26;
}
String+=c;
}
System.out.println (str+ "after encryption:" +string);
}
}
4. Results
Java Third Course Assignment