Create a class, and use the "^" exclusive OR operator in the bit operation to perform the exclusive OR string with a specified value, thereby changing the value of each character of the string, so that an encrypted string can be obtained. When the encrypted string is used as the program input content, and then XOR is performed with the specified value, the encrypted string is restored to the value of the original string.
The code copy is as follows:
import java.util.Scanner;
public class Example {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Please enter an English string or decrypt string");
String password = scan.nextLine();// Get user input
char[] array = password.toCharArray();// Get the character array
for (int i = 0; i < array.length; i++) {// traverse character array
array[i] = (char) (array[i] ^ 20000);// Perform XOR operation on each array element
}
System.out.println("The encryption or decryption result is as follows:");
System.err.println(new String(array));// Output key
}
}
The effect is shown in the figure: