Bit encryption, decryption algorithm

Source: Internet
Author: User
Tags bitwise bitwise operators decrypt

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:;;;;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 (;  Do{System.out.print ("Please enter a key for the encryption and decryption algorithm of the bit:"); N= (). CharAt (0); System.out.print ("Please enter clear text:"); BufferedReader BR=NewBufferedReader (NewInputStreamReader (; 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):"); (); } 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.