XOR Encrypt and Decrypt

XOR Encryption

The XOR logical operation (eXclusive OR) is a logical operator in Boolean algebra. This operator compares two bits and produces a return bit equal to 1 if the compared bits are different, and 0 if they are identical.


XOR encryption is used in many symmetric ciphers, including AES, where the same key is used for both encryption and decryption. The XOR operator is applied bit by bit between the text to be encrypted and the chosen key.

Example of XOR Encryption

Let's take the example of the word "xor" that we want to encrypt with the key "key" (French for key). In binary, it looks like this:


    Xor : 01111000 01101111 01110010
    Key : 01001011 01100101 01111001
    End : 00110011 00001010 00001011

To revert to the initial input ("xor"), you just need to reapply the XOR operator between the output and the key. The XOR function is commutative. If the key is shorter than the input, it is simply reapplied to the chain.


For example, to encrypt the word "bonjour" with the key "cle," you simply repeat the key:


    plain: 01110000 01101100 01100001 01101001 01101110
    keyke: 01001011 01100101 01111001 01001011 01100101

You can see that the shorter the key, the easier it is to break the encryption, as attacks often rely on frequency analysis.


Update cookies preferences