Xor operation for simple string encryption

Source: Internet
Author: User
Tags stringbuffer

Self-lazy, look at the feeling of the reliable

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.