Use an exclusive OR operator to encrypt integers.

Source: Internet
Author: User

Use an exclusive OR operator to encrypt integers.
I. What is the operator:

Binary ^ operators are predefined for integer and bool types. For an integer, ^ returns the bitwise XOR of the calculated operand ". For the bool operand, the logic of the ^ calculated operand is "exclusive or". That is to say, the result is true only when only one operand is true.

Ii. Rules for XOR operation:

If the corresponding bits of both binary numbers are 1 or the corresponding bits of binary numbers are 0, 0 is returned. If one of the two binary numbers is 0 and the other is 1, 1 is returned;

Iii. Execution Process of an exclusive or operation:

Encryption: Performs exclusive or operation on 23, and the key is 15.

Decryption: decrypts the encryption result: 24 by exclusive or operation, key = 15

Note 1: 23-to-2 hexadecimal algorithm flow

Note 2: Compare the exclusive or operation rules of 10111 and 1111:

Note 3: 11000 to 10 hexadecimal number: 1*2 ^ 4 + 1*2 ^ 3 + 0*2 ^ 2 + 0*2 ^ 1 + 0*2 ^ 0 = 16 + 8 + 0 + 0 + 0 = 24

4. Write encryption gadgets:

The main code of the program is as follows:

/// <Summary> /// encryption /// </summary> /// <param name = "sender"> </param> /// <param name =" e "> </param> private void button#click (object sender, eventArgs e) {int num, key; if (int. tryParse (this. textBox1.Text, out key) & int. tryParse (this. textBox2.Text, out num) {this. label4.Text = (num ^ key ). toString ();} else {MessageBox. show ("enter a value", "error ");}}
/// <Summary> /// decrypt /// </summary> /// <param name = "sender"> </param> /// <param name =" e "> </param> private void button2_Click (object sender, eventArgs e) {int key, encrypt; if (int. tryParse (this. textBox1.Text, out key) & int. tryParse (this. label4.Text, out encrypt) {this. label5.Text = (key ^ encrypt ). toString ();} else {MessageBox. show ("enter a value", "error ");}}

 

Related Article

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.