The xor operator is used in Java to encrypt strings

  • 2020-04-01 03:54:02
  • OfStack

To alter the value of each character of a string by performing an xor operation on a specified value with an xor operator symbol, thus obtaining the encrypted string.


import java.util.Scanner; 
public class Encypt { 
  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.out.println(new String(array)); 
  } 
} 

Output results:


 Please enter an English string or decrypt a string : 
www.sohu.com 
 The encryption or decryption results are as follows : 
 I am very tired and very tired  

Conclusion:

Bit operations can perform many advanced, efficient operations. For example, encryption, the n power of multiplication is moving n bits to the right, faster.


Related articles: