||This article needs additional citations for verification. (July 2009)|
- A 0 = A,
- A A = 0,
- (A B) C = A (B C),
- (B A) A = B 0 = B,
where denotes the exclusive disjunction (XOR) operation. This operation is sometimes called modulus 2 addition (or subtraction, which is identical). 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.
For example, the string "Wiki" (01010111 01101001 01101011 01101001 in 8-bit ASCII) can be encrypted with the repeating key 11110011 as follows:
01010111 01101001 01101011 01101001 11110011 11110011 11110011 11110011 = 10100100 10011010 10011000 10011010
And conversely, for decryption:
10100100 10011010 10011000 10011010 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. 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.
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 even in theory.
In any of these ciphers, the XOR operator is vulnerable to a known-plaintext attack, since plaintext ciphertext = key.
Example using Python 2.7 inspired by :
#!/usr/bin/python2.7 from os import urandom def vernam_genkey(length): """Generating a key""" return bytearray(urandom(length)) def vernam_encrypt(plaintext, key): """Encrypting the message. Read more on XOR encryption on http://en.wikipedia.org/wiki/XOR_cipher """ return bytearray([ord(plaintext[i]) ^ key[i] for i in xrange(len(plaintext))]) def vernam_decrypt(ciphertext, key): """Decrypting the message Read more on XOR encryption on http://en.wikipedia.org/wiki/XOR_cipher """ return bytearray([ciphertext[i] ^ key[i] for i in xrange(len(ciphertext))]) def main(): myMessage = """This is a topsecret message...""" print 'message:',myMessage key = vernam_genkey(len(myMessage)) print 'key:', str(key) cipherText = vernam_encrypt(myMessage, key) print 'cipherText:', str(cipherText) print 'decrypted:', vernam_decrypt(cipherText,key) if vernam_decrypt(vernam_encrypt(myMessage, key),key)==myMessage: print ('Unit Test Passed') else: print('Unit Test Failed - Check Your Python Distribution') if __name__ == '__main__': main()
 See also
- Churchhouse, Robert (2002), Codes and Ciphers: Julius Caesar, the Enigma and the Internet, Cambridge: Cambridge University Press, ISBN 978-0-521-00890-7
- Tutte, W. T. (19 June 1998), Fish and I, retrieved 7 October 2010 Transcript of a lecture given by Prof. Tutte at the University of Waterloo