The origin of the replacement encryption and decryption algorithm can be traced back to the time of Caesar (Caesar), where Caesar was said to have invented Caesar's code in order to guarantee the reliability of intelligence. Caesar's password is a simple replacement password, when encrypted, each letter in the alphabet is denoted by the third letter followed, for example, A is denoted by D, B is denoted by E, ... When decrypting, you only need to perform the reverse process.
1. Replace the encryption, decryption algorithm
With the development of this history, the Substitution cipher algorithm scheme has many forms, mainly has the following several kinds:
- Single table instead of cipher algorithm scheme
- The method of replacing cipher algorithm with the same pronunciation
- Multi-table substitution cipher algorithm scheme
- Multi-letter group substitution cipher Algorithm scheme
The following is an example of a single-table instead of a cryptographic algorithm scheme.
1) Replacement encryption algorithm
The basic method of replacing the encryption algorithm is to give a key n, the ASCII code corresponding to each character in the plaintext is moved backwards n and the corresponding ciphertext is obtained.
Follow this idea to write the appropriate encryption algorithm, the code example is as follows:
/*** Encryption Algorithm *@paramstr plaintext String *@paramn the number of bits that each ASCII code moves backwards in plaintext, that is, the key *@return */ Static Char[] Jiami (Char[] str,intN) { intI,len; Char[] Miwen; Len=str.length; Miwen=New Char[Len];//Request Memory for(i=0;i<len;i++) {Miwen[i]=(Char) (str[i]+N); } returnMiwen; }
2) Replacement decryption algorithm
The basic method of replacing the decryption algorithm is to first give a key n, which must be the same as the encryption key, the ASCII code corresponding to each character in the ciphertext moves forward N and gets the corresponding plaintext.
Follow this idea to write the appropriate encryption algorithm, the code example is as follows:
/*** Decryption Algorithm *@paramMiwen Ciphertext String *@paramn the number of bits to move forward for each ASCII code in the ciphertext, which should be the same as the number of bits moved backwards in the encryption algorithm *@return */ Static Char[] Jiemi (Char[] Miwen,intN) { intI,len; Char[] str; Len=miwen.length; STR=New Char[Len];//Request Memory for(i=0;i<len;i++) {Str[i]=(Char) (miwen[i]-N); } returnstr; }
2. Replace the encryption, decryption algorithm instance
The complete program code example is as follows:
PackageCom.cn.mimaxue;ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.util.Scanner;//replacement encryption and decryption algorithm Public classTHJMJM {/*** Encryption Algorithm *@paramstr plaintext String *@paramn the number of bits that each ASCII code moves backwards in plaintext, that is, the key *@return */ Static Char[] Jiami (Char[] str,intN) { intI,len; Char[] Miwen; Len=str.length; Miwen=New Char[Len];//Request Memory for(i=0;i<len;i++) {Miwen[i]=(Char) (str[i]+N); } returnMiwen; } /*** Decryption Algorithm *@paramMiwen Ciphertext String *@paramn the number of bits to move forward for each ASCII code in the ciphertext, which should be the same as the number of bits moved backwards in the encryption algorithm *@return */ Static Char[] Jiemi (Char[] Miwen,intN) { intI,len; Char[] str; Len=miwen.length; STR=New Char[Len];//Request Memory for(i=0;i<len;i++) {Str[i]=(Char) (miwen[i]-N); } returnstr; } Public Static voidMain (string[] args)throwsIOException {Char[] Str,miwen,jiemi; intN,i; String go; System.out.println ("Replacement encryption decryption algorithm demo!" "); Scanner input=NewScanner (system.in); Do{System.out.print ("Please enter a key to replace the cryptographic decryption algorithm:"); N=Input.nextint (); System.out.print ("Please enter clear text:"); BufferedReader BR=NewBufferedReader (NewInputStreamReader (system.in)); String strtemp=Br.readline (); STR=Strtemp.tochararray (); System.out.print ("Clear text is:"); for(i=0;i<str.length;i++) {System.out.print (str[i]); } System.out.println (); Miwen=Jiami (str,n); System.out.print ("Ciphertext is:"); for(i=0;i<miwen.length;i++) {System.out.print (miwen[i]); } System.out.println (); Jiemi=Jiemi (miwen,n); System.out.print ("Decrypt as:"); for(i=0;i<jiemi.length;i++) {System.out.print (jiemi[i]); } System.out.println (); System.out.print ("Whether to continue (y/n):"); Go=Input.next (); } while(Go.equalsignorecase ("Y")); System.out.println ("Quit the program!" "); }}
The results of the program run as follows:
Replacement Encryption decryption algorithm demo! Enter the key for the replacement encryption and decryption algorithm:4 Please enter clear text: Hello everyone! Clear Text is: Hello everyone! ciphertext: Lipps$iziv}sri% decrypted as: Hello everyone! Continue (y/N): Y Enter the key for the replacement encryption decryption algorithm:3 Enter clear text:One world, one dream! Clear text:One world, one dream! ciphertext: rqh#zruog/#Rqh #guhdp$ decryption:One World, one dream! whether to continue (Y/N): N Exit the program!
Replace encryption, decryption algorithm