Simple data encryption and decryption, simple data encryption and decryption

Source: Internet
Author: User

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:

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.