Simple data encryption and decryption, simple data encryption and decryption
Simple data encryption and decryption
Generally, the "^" exclusive or operation is used to encrypt strings.
Use char [] array = String. toCharArray (); // to obtain the character array;
Then, it traverses the character array and performs the exclusive or operation.
The bitwise XOR operation is: the bitwise value is the same as 1, and the bitwise value is the same as 0.
The original text and the key are used for exclusive or calculation to obtain the ciphertext, Which is encrypted.
You can obtain the original text by performing exclusive or operations on the same password and key. This is decryption.
The Code is as follows:
1 import java. io. *; 2 3 public class Example {4 public static void main (String [] args) {5 char a [] = "I am really handsome ". toCharArray (); 6 int n = 0; 7 try {8 File out = new File ("word.txt"); 9 for (int I = 0; I <. length; I ++) {10 a [I] = (char) (a [I] ^ 'R'); 11} 12 FileWriter fw = new FileWriter (out ); 13 fw. write (a, 0,. length); 14 fw. close (); 15 FileReader fr = new FileReader (out); 16 char tom [] = new char [1 0]; 17 System. out. println ("encrypted:"); 18 while (n = fr. read (tom, 0, 10 ))! =-1) {19 String s = new String (tom, 0, n); 20 System. out. println (s); 21} 22 fr. close (); 23 fr = new FileReader (out); 24 System. out. println ("plaintext:"); 25 while (n = fr. read (tom, 0, 10 ))! =-1) {26 for (int j = 0; j <n; j ++) {27 tom [j] = (char) (tom [j] ^ 'R'); 28} 29 String str = new String (tom, 0, n); 30 System. out. println (str); 31} 32 33 fr. close (); 34} catch (Exception e) {35 e. printStackTrace (); 36} 37 38} 39 40}
The encrypted result is as follows: