13
3
Background
A one-time pad is a form of encryption that has been proven impossible to crack if used properly.
Encryption is performed by taking a plaintext (comprised of only letters A-Z) and generating a random string on the same length (also only letters). This string acts as the key. Each character in the plaintext is then paired up with the corresponding character in the key. The ciphertext is computed as follows: For each pair, both characters are converted to numbers (A=0, B=1, ... Z=25). The two numbers are added modulo 26. This number is the converted back into a character.
Decryption is exactly the opposite. The characters in the ciphertext and key are paired up and converted to numbers. The key is then subtracted from the ciphertext modulo 26, and the result is converted back into a character A-Z.
The Challenge
Your challenge is to write the shortest program possible that can both encrypt and decrypt a one-time pad.
On the first line of input (to STDIN), there will be either the word "ENCRYPT" or the word "DECRYPT".
If the word is encrypt, then the next line will be the plaintext. Your program should output two lines (to STDOUT), the first being the key, and the second being the ciphertext.
If the word is decrypt, your program will get two more line of input. The first line will be the key, and the second line will be the ciphertext. You program should output one line, which will be the plaintext that has been decrypted.
The plaintext, ciphertext, and key should always consist of uppercase letters A-Z. They will always be a single line and contain no whitespace.
The key should always be random. No large parts of it should repeat between runs, and there should be no patterns that can be found in the text.
Two simple examples:
ENCRYPT
HAPPYBIRTHDAY
>ABKJAQLRJESMG
>HBZYYRTICLVME
DECRYPT
ABKJAQLRJESMG
HBZYYRTICLVME
>HAPPYBIRTHDAY
The >
represents which lines are output, so you don't have to print that symbol as output.
7Not to criticize the challenge on it's own merits (which are fine), but I am going to criticize the cryptography here. What you are describing is a "stream cipher" as it depends on a PRNG (unless your computer has access to a source or real randomness (and if the linux implementation of /dev/urandom counts is a matter of some debate)), and having the key developed at enciphering time defeats the only really good use for a OTP which is time shifting of secure communications. – dmckee --- ex-moderator kitten – 2012-03-16T22:45:20.700
1Also, all challenges are language agnostic by default, so I've removed that tag. – dmckee --- ex-moderator kitten – 2012-03-16T22:46:33.023
7@dmckee Regarding your first comment, I agree, which is why I do not intend to use these answers to secure my communications. – PhiNotPi – 2012-03-16T23:04:39.113
1
this would have been more fun IMO to leave randomness out of the problem; given a source of randomness (
– ixtmixilix – 2013-03-03T20:59:29.357/dev/random
,haveged
), encrypt by xoring the ords with the bytes and decrypt by xoring them with the key. https://gist.github.com/5078264 the key or the randomness can be read from stdin, the message or cyphertext can be a filename argument.@PhiNotPi I have a suggestion. Why not give a bonus if they use a truly random source (like
/dev/hwrng
, instead of using pseudo random (which technically makes it broken.) – PyRulez – 2014-08-01T16:05:16.683