A little while ago, I wrote a simple one-time pad utility in JavaScript that can be used to send encrypted messages with relative security. One-time-pad is technically cryptographically secure, however it’s only as secure as how it’s used and the strength of the random number generator used to create the pad. My script must rely on the strength of the psudo random number generator of the browser scripting engine, so security is relatively weak and this must be taken into consideration before using it for anything important.
I’ve been getting some emails asking about using the utility and I think there was some added confusion since my version uses numbers as well. We’ll here’s a little tutorial on how to use a one-time pad…
Encrypting the message
Take the following simple message for example, and take the corresponding number from the values list on the one-time-pad.
M | E | R | R | Y | C | H | R | I | S | T | M | A | S |
↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ |
13 | 05 | 18 | 18 | 25 | 03 | 08 | 18 | 09 | 19 | 20 | 13 | 01 | 19 |
Now, using the one-time pad, I got the following set of random characters and their corresonding values.
D | 9 | D | F | D | B | L | M | L | W | W | 8 | 8 | A |
↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ |
04 | 35 | 04 | 06 | 04 | 02 | 12 | 13 | 12 | 23 | 23 | 34 | 34 | 01 |
If I take the corresponding value of each character from both lists and add them together, I can get the new ciphertext that can be sent securely to the recipient. Obviously we can’t use the numbers in green because our list of available characters only go up to 36, however, we can subtract 36 from these numbers (mod 36) so it will cycle back to the available pool.
13 | 05 | 18 | 18 | 25 | 03 | 08 | 18 | 09 | 19 | 20 | 13 | 01 | 19 | + |
04 | 35 | 04 | 06 | 04 | 02 | 12 | 13 | 12 | 23 | 23 | 34 | 34 | 01 | |
17 | 40 | 22 | 24 | 29 | 05 | 20 | 31 | 21 | 42 | 43 | 47 | 35 | 20 | = |
17 | 04 | 22 | 24 | 29 | 05 | 20 | 31 | 21 | 06 | 07 | 11 | 35 | 20 | mod 36 |
This new set of numbers can be turned into the ciphertext using that same values list.
17 | 04 | 22 | 24 | 29 | 05 | 20 | 31 | 21 | 06 | 07 | 11 | 35 | 20 |
↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ |
Q | D | V | X | 3 | E | T | 5 | U | F | G | K | 9 | T |
Decrypting the message
So, our recipient got the encrypted ciphertext, QDVX3ET5UFGK9T, and now he/she must take these charaters and turn them into a list of values in order to decrypt it.
Q | D | V | X | 3 | E | T | 5 | U | F | G | K | 9 | T |
↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ |
17 | 04 | 22 | 24 | 29 | 05 | 20 | 31 | 21 | 06 | 07 | 11 | 35 | 20 |
Then, using the same one-time pad key values (since they’ll both be using the same sheet) the recipient will get the random characters and their associated values. Same as above.
D | 9 | D | F | D | B | L | M | L | W | W | 8 | 8 | A |
↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ |
04 | 35 | 04 | 06 | 04 | 02 | 12 | 13 | 12 | 23 | 23 | 34 | 34 | 01 |
The recipient will then subtract the one-time pad values from the encrypted message text. Basically the reverse of what the sender did. Again, this may result in some numbers going out of bounds of the available values list and become negative (marked in red), which we can fix by adding 36, again the opposite of what we did above, to bring it back to the available character pool.
17 | 04 | 22 | 24 | 29 | 05 | 20 | 31 | 21 | 06 | 07 | 11 | 35 | 20 | – |
04 | 35 | 04 | 06 | 04 | 02 | 12 | 13 | 12 | 23 | 23 | 34 | 34 | 01 | |
13 | 31 | 18 | 18 | 25 | 03 | 08 | 18 | 09 | 17 | 16 | 23 | 01 | 19 | = |
13 | 05 | 18 | 18 | 25 | 03 | 08 | 18 | 09 | 19 | 20 | 13 | 01 | 19 | mod 36 |
And when we turn those numbers back into the original text using the values list, we get :
13 | 05 | 18 | 18 | 25 | 03 | 08 | 18 | 09 | 19 | 20 | 13 | 01 | 19 |
↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ | ↓ |
M | E | R | R | Y | C | H | R | I | S | T | M | A | S |
… And a very Happy New Year!
Pingback: Cryptographically secure One-time Pads « This page intentionally left ugly