Caesar encryption and decryption algorithms, and Caesar decryption algorithms
The ancient Roman Emperor Caesar used the following methods to encrypt Military Intelligence during the war. The main method is misplacement of letters. The following figure shows the encryption method with three wrong digits for encryption.
(1) Design Philosophy:
(2) program flowchart:
(3) source code:
Package temp; import java. util. encryption; public class Tast2 {public static char [] encrypation (String str) {// encryption method char chararry [] = str. toCharArray (); // converts a string to a character array for (int I = 0; I <str. length (); I ++) {// when the last three digits of the alphabet are read, the encrypted string is the first three digits of the alphabet if (str. charAt (I)> = 88 & str. charAt (I) <= 90) {// chararry [I] = (char) (str. charAt (I)-23);} else if (str. charAt (I) >=120 & str. charAt (I) <= 122) {// chararry [I] = (char) (str. charAt (I)-23);} // General encryption method else {chararry [I] = (char) (str. charAt (I) + 3) ;}} return chararry;} public static char [] deciphering (String str) {// j Decryption Method char chararry [] = str. toCharArray (); for (int I = 0; I <str. length (); I ++) {// when reading the first three digits of the alphabet, the encrypted string is the last three digits of the alphabet if (str. charAt (I)> = 65 & str. charAt (I) <= 67) {// chararry [I] = (char) (str. charAt (I) + 23);} else if (str. charAt (I)> = 97 & str. charAt (I) <= 99) {// chararry [I] = (char) (str. charAt (I) + 23);} // General Decryption Method else {chararry [I] = (char) (str. charAt (I)-3) ;}} return chararry;} public static void main (String [] args) {role imput1 = new role (System. in); required imput2 = new partition (System. in); System. out. println ("*****************************"); System. out. println ("***** encrypted string input --- 1 ---"); System. out. println ("***** decryption string input --- 2 ---"); System. out. println ("*****************************"); System. out. print ("Enter:"); int flag = imput1.nextInt (); System. out. print ("enter an English String:"); string String = imput2.nextLine (); if (flag = 1) {System. out. print ("encrypted:"); for (int I = 0; I <string. length (); I ++) {System. out. print (Tast2.encrypation (string) [I]) ;}} if (flag = 2) {System. out. print ("decrypted:"); for (int I = 0; I <string. length (); I ++) {System. out. print (Tast2.deciphering (string) [I]) ;}} imput1.close (); imput2.close ();}}
(4) Result:
Appendix: common methods for sorting String types:
(1) String. equals ():
For example, str1.equals (str2). If the content of string st1 is the same as that of string str2, true is returned; otherwise, false is returned.
(2) String. length ():
For example, str1.length (); returns the length or size of str1.
(3) String. charAt ():
For example: str1.charAt (index); returns the character marked as index in str1. The subscript starts from 0.
(4) String. getChars ():
For example: str1.getChars (, charArray, 0); copy the substring from subscript 0 to subscript 5 in str1 to the character array charArray. The starting subscript of the character array is 0.
(5) Sting. replace ():
For example, str1.replace (oldChar, newChar); returns a new character with the same character as str1, but replaces each oldCase character with newCase.
(6) Sting. toUpperCase ():
For example, str1.toUpperCase (); returns a new character with the same character as str1, but converts all lowercase letters to uppercase letters.
(7) Sting. toLowerCase ():
For example, str1.toLowerCase (); returns a new character with the same character as str1, but converts all uppercase letters to lowercase letters.
(8) Sting. trim ():
For example, str1.trim (); returns a new character with the same character as str1, but removes the leading and trailing spaces of str1, (blank characters include spaces, tabs, and transposed characters)
(9) String. toCharArray ():
For example, chararray [] = str1.toCharArray (); converts string str1 to a character array and stores it in chararrayp [].