HOME

TheInfoList



OR:

In
cryptography Cryptography, or cryptology (from grc, , translit=kryptós "hidden, secret"; and ''graphein'', "to write", or ''-logia'', "study", respectively), is the practice and study of techniques for secure communication in the presence of adve ...
, the simple XOR cipher is a type of ''additive cipher'', an
encryption algorithm In cryptography, encryption is the process of encoding information. This process converts the original representation of the information, known as plaintext, into an alternative form known as ciphertext. Ideally, only authorized parties can dec ...
that operates according to the principles: :A \oplus 0 = A, :A \oplus A = 0, :A \oplus B = B \oplus A, :(A \oplus B) \oplus C = A \oplus (B \oplus C), :(B \oplus A) \oplus A = B \oplus 0 = B, where \oplus 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.


Example

For example, the string "Wiki" ( in 8-bit
ASCII ASCII ( ), abbreviated from American Standard Code for Information Interchange, is a character encoding standard for electronic communication. ASCII codes represent text in computers, telecommunications equipment, and other devices. Because ...
) 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 (i.e. using the same key for xor operation on the whole data) 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 In cryptography, the one-time pad (OTP) is an encryption technique that cannot be cracked, but requires the use of a single-use pre-shared key that is not smaller than the message being sent. In this technique, a plaintext is paired with a r ...
, which is unbreakable in theory. The XOR operator in any of these ciphers is vulnerable to a
known-plaintext attack The known-plaintext attack (KPA) is an attack model for cryptanalysis where the attacker has access to both the plaintext (called a crib), and its encrypted version ( ciphertext). These can be used to reveal further secret information such as s ...
, since ''plaintext'' \oplus ''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. Other logical operations such and AND or OR do not have such a mapping (for example, AND would produce three 0's and one 1, so knowing that a given ciphertext bit is a 0 implies that there is a 2/3 chance that the original plaintext bit was a 0, as opposed to the ideal 1/2 chance in the case of XOR)


Example implementation

Example using the Python programming language. from os import urandom def genkey(length: int) -> bytes: """Generate key.""" return urandom(length) def xor_strings(s, t) -> bytes: """Concate xor two strings together.""" if isinstance(s, str): # Text strings contain single characters return "".join(chr(ord(a) ^ b) for a, b in zip(s, t)).encode('utf8') else: # Bytes objects contain integer values in the range 0-255 return bytes( ^ b for a, b in zip(s, t) message = 'This is a secret message' print('Message:', message) key = genkey(len(message)) print('Key:', key) cipherText = xor_strings(message.encode('utf8'), key) print('cipherText:', cipherText) print('decrypted:', xor_strings(cipherText, key).decode('utf8')) # Verify if xor_strings(cipherText, key).decode('utf8')

message: print('Unit test passed') else: print('Unit test failed')


See also

*
Block cipher In cryptography, a block cipher is a deterministic algorithm operating on fixed-length groups of bits, called ''blocks''. Block ciphers are specified cryptographic primitive, elementary components in the design of many cryptographic protocols and ...
* Vernam cipher * Vigenère cipher


References


Notes


Citations


Sources

* * * * * * * * Transcript of a lecture given by Prof. Tutte at the
University of Waterloo The University of Waterloo (UWaterloo, UW, or Waterloo) is a public research university with a main campus in Waterloo, Ontario, Canada. The main campus is on of land adjacent to "Uptown" Waterloo and Waterloo Park. The university also operates ...
{{refend Stream ciphers