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 ");}}