Bit encryption and decryption algorithm will convert plaintext information into binary data, and then encrypt these bits to get ciphertext. Bit encryption algorithm relies on the powerful bit processing ability of computer, which is very popular in practical application. Many encryption and decryption algorithms in modern cryptography rely on bit encryption and decryption ideas, for example, a very popular sequence cipher scheme.
1. Bit encryption, decryption algorithm
In the Java language, 6 bit operators are provided, as shown in the following table. In cryptography, you can choose the appropriate bitwise operators to encrypt and decrypt as needed. In general, it is more convenient to use XOR or operation.
Bit arithmetic |
Name |
& |
Bitwise VS (and) |
| |
bitwise OR (OR) |
^ |
Bitwise XOR OR (XOR) |
~ |
Negate (not) |
<< |
Move left |
>> |
Move right |
One advantage of using XOR operators for encryption and decryption is that, in binary operations, if a plaintext bits and key are bitwise "XOR", the ciphertext will be obtained, and the cipher will be re-bitwise "XOR" with the key, and the plaintext can be obtained. In this way, you can simply write a function to complete both the encryption and decryption operations.
The corresponding bit encryption and decryption algorithm can be written according to this idea, and the code example is as follows:
/*** bit encryption and decryption algorithm *@paramstr plaintext (ciphertext) *@paramN Key *@returnciphertext (Clear text)*/ Static Char[] Bitcode (Char[] str,CharN) { intLen,i; Char[] Wen; Len=str.length; Wen=New Char[Len]; for(i=0;i<len;i++) {Wen[i]=(Char) (str[i]^N); } returnWen; }
2. Bit encryption, decryption algorithm example
The complete program code example is as follows:
PackageCom.cn.mimaxue;ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.util.Scanner;//bit encryption, decryption algorithm Public classBitjiami {/*** bit encryption and decryption algorithm *@paramstr plaintext (ciphertext) *@paramN Key *@returnciphertext (Clear text)*/ Static Char[] Bitcode (Char[] str,CharN) { intLen,i; Char[] Wen; Len=str.length; Wen=New Char[Len]; for(i=0;i<len;i++) {Wen[i]=(Char) (str[i]^N); } returnWen; } Public Static voidMain (string[] args)throwsIOException {Char[] Str,miwen,jiemi; inti; CharN; String go; System.out.println ("Bit encryption decryption algorithm demo!" "); Scanner input=NewScanner (system.in); Do{System.out.print ("Please enter a key for the encryption and decryption algorithm of the bit:"); N= Input.next (). CharAt (0); 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=Bitcode (str,n); System.out.print ("Ciphertext is:"); for(i=0;i<miwen.length;i++) {System.out.print (miwen[i]); } System.out.println (); Jiemi=Bitcode (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:
bit encryption decryption algorithm demo! Please enter the key for the encryption and decryption algorithm:5 Please enter clear text: Hello everyone! Clear Text is: Hello everyone! the ciphertext is:} Pyyzpcpglz[p decryption as: Hello everyone! Continue (y/N): Y Enter the key for the bit encryption decryption algorithm:9 Enter clear text: The Big Bang Theory! Clear text: The Big Bang Theory! ciphertext: mq\[p^[xw^mq\[email protected] decryption: The Big Bang Theory! Whether to continue (Y/N): N Exit the program!
Bit encryption, decryption algorithm