Requirements:
* Encrypt the value of each character entered by the user, and the decrypted string output
* Decrypt and output the encrypted string entered by the user
Implementation code:
ImportJava.util.Scanner;/*** Required: * 1. Encrypt the value of each character entered by the user, and output the decrypted string as * 2. Decrypts the encrypted string entered by the user and outputs *@authorAdministration **/ Public classEncryption { Public Static voidMain (string[] args) {Scanner input=NewScanner (system.in); System.out.println ("Please enter an English string or a secret string"); //Get user InputString Password =Input.nextline (); //talking about getting the string into a character array Char[] C =Password.tochararray (); //encrypt a character array with a For loop for(inti=0;i<c.length;i++) {C[i]= (Char) (c[i]^20000); } //output encryption or decryption resultsSystem.out.println ("After encrypting or decrypting the result is as follows:"); System.out.println (NewString (c)); }}
Operation Result:
* Encryption Process:
Please enter an English string or a secret string: I Love you after encryption or decryption results are as follows: Planchette a 乬 long 乹 spent 乕
* Decryption Process:
Please enter an English string or a secret string: Planchette a 乬 long time a 乹 spent 乕 encryption or decryption results as follows: I Love you
Principle Analysis:
The key technique of this example is the XOR operation. Principle: If a character (or value) A and a value B are different or operation to get C, then use C and B for later operations can be restored to a.
Java small case--encrypt and decrypt a string