Encrypt with XOR

2010-01-17

XOR (eXclusive OR) is a binary operator that compares two binary numbers and generate a new value depending on the two numbers that were entered. XOR adds two binary numbers, divide them by two and returns the remainder of the division. If both numbers are equal, 1 and 1 or 0 and 0, it generates 0. If both numbers are different, it generates 1.

0 XOR 0 = 0
0 XOR 1 = 1
1 XOR 0 = 1
1 XOR 1 = 1

How this can be used for encryption

XOR can encrypt data with a password. XOR is a symmetrical encryption method which needs a key that’s as long as the raw data. In common implementations of this, a key stream is used. It generates a long key from the shorter password. The encryption works so that the first binary bits of both the raw data and the key stream are compared with XOR to generate a new binary bit. This process is repeated for each binary bit that is left of the raw data. An example is shown here:

To decrypt the encrypted data, the same method is used. But this time you input the encrypted data together with the same key stream as before. If the password is correct, you should get the same raw data as before the encryption. An example is shown here: