XOR cipher


In cryptography, the simple XOR cipher is a type of additive cipher, an encryption algorithm that operates according to the principles:
For example where denotes the exclusive disjunction operation. This operation is sometimes called modulus 2 addition. With this logic, a string of text can be encrypted by applying the bitwise XOR operator to every character using a given key. To decrypt the output, merely reapplying the XOR function with the key will remove the cipher.

Example

The string "Wiki" can be encrypted with the repeating key as follows:
And conversely, for decryption:

Use and security

The XOR operator is extremely common as a component in more complex ciphers. By itself, using a constant repeating key, a simple XOR cipher can trivially be broken using frequency analysis. If the content of any message can be guessed or otherwise known then the key can be revealed. Its primary merit is that it is simple to implement, and that the XOR operation is computationally inexpensive. A simple repeating XOR cipher is therefore sometimes used for hiding information in cases where no particular security is required. The XOR cipher is often used in computer malware to make reverse engineering more difficult.
If the key is random and is at least as long as the message, the XOR cipher is much more secure than when there is key repetition within a message. When the keystream is generated by a pseudo-random number generator, the result is a stream cipher. With a key that is truly random, the result is a one-time pad, which is unbreakable in theory.
The XOR operator in any of these ciphers is vulnerable to a known-plaintext attack, since plaintext ''ciphertext = key''.
It is also trivial to flip arbitrary bits in the decrypted plaintext by manipulating the ciphertext.
This is called malleability.

Usefulness in cryptography

The primary reason XOR is so useful in cryptography is because it is "perfectly balanced"; for a given plaintext input 0 or 1, the ciphertext result is equally likely to be either 0 or 1 for a truly random key bit.
The table below shows all four possible pairs of plaintext and key bits. It is clear that if nothing is known about the key or plaintext, nothing can be determined from the ciphertext alone.
PlaintextKeyCiphertext
000
011
101
110

Other logical operations such and AND or OR do not have such a mapping. For example consider the table for AND below:
PlaintextKeyCiphertext
000
010
100
111
If the ciphertext were 0, then there is a 2/3 chance that the plaintext was 0 too. And if the ciphertext was 1, then the plaintext would have to be 1. This clearly reveals information about the text that the XOR approach does not.

Example implementation

Example using the JavaScript programming language.
function xor_Encrypt
function xorDecrypt
Another example using the Python programming language.

from os import urandom
def generate_key -> bytes:
"""Generate encryption key."""
return urandom
def xor_strings -> bytes:
"""Concatenate xor two strings together."""
if isinstance:
# Text strings contain single characters
return "".join).encode
else:
# Bytes objects contain integer values in the range 0-255
return bytes
message = "This is a secret message"
print
key = generate_key
print
cipherText = xor_strings
print
print.decode)
  1. Verify
if xor_strings.decode message:
print
else:
print

A shorter example using the R programming language, based on a posted on Instagram by GCHQ.

secret_key <- c |> as.raw
secret_message <- "I <3 Wikipedia" |>
charToRaw |>
xor |>
base64enc::base64encode
secret_message_bytes <- secret_message |>
base64enc::base64decode
xor |> rawToChar