# XOR + OTP encryption demo

##### By Karlheinz Niebuhr, follow me on twitter, and github

During the Stanford Crypto-course I made a demo about OTP encryption using the boolean “exclusive or” (XOR) function in order to better understand the concept behind it. What is XOR?

In digital cryptography basically everything comes down to XOR’ing things. It got my attention because his is such a simple concept but can be literally unbreakable when combined with OTP encryption. OTP is the only mathematically proved unbreakable encryption.

Now you may ask why I used OTP encryption in my demo. It turns out that OTP can be implemented with little code and therefore is perfect for a XOR example.

### One-time pad encryption

A one-time pad (OTP) is an encryption technique that cannot be cracked if used correctly. In this technique, a plaintext is paired with a random secret key (or pad). Then, each bit or character of the plaintext is encrypted by combining it with the corresponding bit or character from the pad using modular addition. If the key is truly random, is at least as long as the plaintext, is never reused in whole or in part, and is kept completely secret, then the resulting ciphertext will be impossible to decrypt or break.[1]

To find key or plaintext, an adversary only has the random ciphertext at his disposal. This is an equation with two unknowns (the key and the message), which is mathematically unsolvable.

If someone had infinite computational power he could go through all possible keys (a brute force attack). He would find out that applying the key XVHEU on ciphertext QJKES would produce the (correct) word TODAY. Unfortunately, he would also find out that the key FJRAB would produce the word LATER, and even worse, DFPAB would produce the word NEVER. He has no idea which key is the right one. In fact, you can produce any desired word or phrase from any one-time pad -encrypted message, as long as you use the ‘right’ wrong key. There is no way to verify if a solution is the right one. Therefore, the one-time pad system is proven completely secure.[2]

For the keen readers: THis is a more extensive explanation about OTP and the history behind.

**So all this was very exiting so I decided to make a proof of concept. This is what I came up with.**

Check out my code on Github or just try it online.

The script takes a keyboard input from the user (**message**), and generates a random password with the **same length**. *Note that it’s important that the password has the same length than the message.*

This is the entire script

The function that does the encryption is the following. It takes two bit strings as input (the message and the key), iterates over them and applies XOR to every bit. This is symbolised by ⊕ and is represented by the following “truth table”, where + represents “true” or 1 and − represents “false” or 0.

INPUT | OUTPUT | |

A | B | A ⊕ B |

− | − | − |

− | + | + |

+ | − | + |

+ | + | − |

This code snippet generates the random key, we assume this to be a truly random string.

These functions convert ASCII to Binary and vice versa.

Output

Feel free to experiment with the code, I hope someone will find this as useful as I did to get a better understanding of the XOR encryption process. Questions are welcome. https://github.com/Karlheinzniebuhr/XOR-encryption-demo/

###**Update** After some great feedback on Reddit I made some updates to the code, namely switching to os.urandom() which is better suited for cryptographic use than random.choice().

Sources:

[1] http://users.telenet.be/d.rijmenants/en/onetimepad.htm

[2] http://en.wikipedia.org/wiki/One-time_pad

## Comments