Computers do not operate on words, but on sequences of zeros and ones. Whether text, image, sound, video or program is represented in a computer as a sequence of bits. The operation of mapping something from the real world to a sequence of bits is called encoding.
And the encryption operation of the program, is to represent the plaintext of the sequence of bits into a sequence of ciphertext.
XOR stands for exclusive or, which is called exclusive or in Chinese.
1 XOR operation (1 bit)
The 1-bit XOR operation rules are as follows:
- 0 XOR 0 = 0
- 0 XOR 1 = 1
- 1 XOR 0 = 1
- 1 XOR 1 = 0
If you understand 0 as an even number and 1 as an odd number, you can equate XOR with general addition.
- 0 (even) + 0 (even) = 0 (even)
- 0 (even) + 1 (odd) = 1 (odd)
- 1 (odd) + 0 (even) = 1 (odd)
- 1 odd + 1 odd = 0 even
Because XOR is very similar to addition, XOR is usually denoted by the sign ⊕.
- 0 star 0 is equal to 0
- 0 star 1 is 1
- Student: 1 star 0 is 1
- Student: 1 star 1 is 0
2 XOR operation (bit sequence)
If it is an XOR between sequences of long bits, it is only necessary to perform an XOR on each corresponding bit. Assuming that the long bit sequence A is 10100 and the long bit sequence B is 10011, the operation process of A ⊕ B is as follows:
Unlike addition, there is no carry required in XOR.
Since the result of an XOR operation on two identical numbers must be 0, if the result of A⊕ B is XOR operation with B, the result will change back to A. In other words, the B’s in these two equations will cancel each other out, so (A ⊕ B) ⊕ B is equal to A.
The above calculation process and encryption, decryption process is very similar.
- Encryption: Encrypt plaintext A with key B to obtain the ciphertext A ⊕ B;
- Decryption: Decrypt the ciphertext A ⊕ B with key B to obtain plaintext A.
In fact, it is possible to achieve a strong password using XOR alone, as long as you choose a suitable B.