Java bit operation encryption example

  • 2020-04-01 02:53:35
  • OfStack

Creates a class that performs an xor operation on a string with a specified value using the "^" xor operator in a bit operation to change the value of each character in the string. This results in an encrypted string. When the encrypted string as the program input content, and then the specified value of xor operation, the encrypted string to restore the value of the original string.


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 a string ");
        String password = scan.nextLine();//Get user input
        char[] array = password.toCharArray();//Get the array of characters
        for (int i = 0; i < array.length; i++) {//Traverses the array of characters
            array[i] = (char) (array[i] ^ 20000);//Xor operations are performed on each array element
        }
        System.out.println(" The encryption or decryption results are as follows: ");
        System.err.println(new String(array));//The output key
    }
}

The effect is shown in the figure:

< img border = 0 id = theimg onclick = window. The open this. (SRC) SRC = "/ / img.jbzj.com/file_images/article/201402/20140208164731.jpg? 201418164827 ">

< img border = 0 id = theimg onclick = window. The open this. (SRC) SRC = "/ / img.jbzj.com/file_images/article/201402/20140208164753.jpg? 201418164916 ">

Related articles: