XOR cipher

From Wikipedia, the free encyclopedia

Jump to: navigation, search

In cryptography, a simple XOR cipher is a relatively simple encryption algorithm that operates according to the principles:

A \oplus 0 = A,
A \oplus A = 0,
(B \oplus A) \oplus A = B \oplus 0 = B,

where \oplus denotes the exclusive disjunction (XOR) operation. 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 key will remove the cipher.

For example, the string "Wiki" (01010111 01101001 01101011 01101001 in 8-bit ASCII) can be encrypted with the key 11110011 as follows:

01010111 01101001 01101011 01101001
\oplus 11110011 11110011 11110011 11110011
= 10100100 10011010 10011000 10011010

And conversely, for decryption:

10100100 10011010 10011000 10011010
\oplus 11110011 11110011 11110011 11110011
= 01010111 01101001 01101011 01101001

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. Its primary merit is that it is simple to implement, and that the XOR operation is computationally inexpensive. A simple XOR cipher is therefore sometimes used for hiding information in cases where no particular security is required (cf. ROT13). Also, the XOR cipher is completely vulnerable to the known-plaintext attack, since plaintext \oplus ciphertext = key.

However, if the key is as long as the message (so it is never repeated) and its bits are random, and is used only once, it is a one-time pad, which is unbreakable, even in theory.

[edit] See also

Personal tools
Languages