How to use XOR operation for simple encryption and decryption

Source: Internet
Author: User
Tags sca stringbuffer

Encrypt a string using the "^" XOR operation

Idea: 1. First create a string input scanner;

2. Via char[] array = Password.tochararray ();//get the character array;

3. Iterate through the character array and use the traversal as it is currently understood: all elements of the array are accessed, such as when you want to output all the information in the array, you need to use

4. Perform an XOR operation

Bitwise "XOR" is: bit value is equal to 1, different 0

For example:

< encryption process:>

The original interpretation of the binary is 1 1 0 0----Original

The binary of the set key is 0 1 1 0----key

Both do "XOR" results for 0 1 0 1----ciphertext

< decryption process:>

0 1 0 1----ciphertext

0 1 1 0----key

Both "XOR" is obtained in the original text 1 1 0 0----Original

Detailed code:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Package Com.lixiyu; Import Java.util.Scanner; public class Example {public static void main (string[] args) {Scanner sca=new Scanner (system.in); System.out.println ("Please enter an English string or decrypt the string"); String line=sca.nextline ();//Get user input information char[] Array=line.tochararray ()//Get character array for (int i=0;i<array.length;i++) {/ /Calendar character array array[i]= (char) (array[i]^20000);//xor for each element of the array} System.out.println ("Encrypted decryption results are as follows:"); System.out.println (new String (array));//Output Key}}

XOR operation:

1 ^ 1 = 0

1 ^ 0 = 1

0 ^ 1 = 1

0 ^ 0 = 0

The ASCII encoding of the character ' A ' is 65:00000000 01000001

Take integers 7:00000000 00000000 00000000 00000111

After XOR operation: 00000000 00000000 00000000 01000110

The simple cryptographic algorithm code is as follows:

public class Test {public
        static final int KEY = 7;
        public static void Main (string[] args) {
            String str = "Hello world!";
            StringBuffer str2 = new StringBuffer ();  Stores the encrypted string
            stringbuffer STR3 = new StringBuffer ();  Stores the decrypted string
            //Encryption procedure for
            (int i=0;i<str.length (); i++)
            {
                char c = (char) (Str.charat (i) ^ KEY);
                Str2.append (c);
            }
            Decryption process for
            (int i=0;i<str2.length (); i++)
            {
                char c = (char) (Str2.charat (i) ^ KEY);
                Str3.append (c);
            }
            System.out.println ("Original string is:" + str);
            System.out.println ("Encrypted string is:" + str2);
            System.out.println ("Decrypted string is:" + STR3);}
    }

Output:

The original string is: Hello world!

The encrypted string is: Obkkh ' phukc&

The decrypted string is: Hello world!

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.