In this assignment you will implement the One Time Pad cryptosystem and use it to exchange
information with a partner.
You can read online or consult your class notes for a description of the cryptosystem. Here is a
brief summary:
‘The plaintext message is a sequence of bytes corresponding to ASCII characters.
The key is a sequence of bytes generated at random, at least as long as the message.
The ciphertext is the byte-by-byte XOR of the message and the key.
For example, if the message is “HI“ then the plaintext is an array of bytes (chars) of length 2 with
contents amounting to {Ox48,ox49}. The key must be of length 2, generated at random. Suppose it is
{Ox20,oxf3}. Then the ciphertext is {ox48 A Ox20,ox49 A Oxf3} = {Ox60,oxba}, where the “hat” symbol
denotes XOR.
You must implement Gen, Enc, and Dec for this cryptosystem. Here is a high level description of these
functions, which you must implement in your langUage of choice:
Gen: This is the key generation program. It should generate a key of a specified length (passed in
as an input parameter). The key is an array of random bytes generated by a cryptographically secure
random number generator. The rand() library function is not cryptographically secure. One way to get
true random data is to use /dev/random or /dev/urandom on a Linux machine. Code for doing this can be
found here:
http://stackoverflow.com/questions/2572366/how-to-use-dev-random-or-urandom-in-c
For the purposes of getting random data you are allowed to copy and past this code from the internet
(or other code that you might find by Googling). Generating the key will take some time (this is typical
in cryptography). You may use other ways of getting random data, but you must convince me that they are
cryptographically secure.
Once you have the required number of random bytes, the Gen function should write them to a file named
keyfile”. This is all Gen must do.
Enc: The encryption function reads two files as input. One is the keyfile and one is the plaintext
file. It performs the encryption by XORing the ciphertext and plaintext byte by byte as described
above. It outputs the resulting ciphertext to an output file called “ciphertext”. Remember that the
keyfile must be at least as long (in characters/bytes) as the plaintext.
Dec: The decryption function is the same as the encryption function, except that you give it the
ciphertext instead of the plaintext, along with the key.
Implement the functions described above to create your cryptosystem. Then use your cryptosystem to
exchange a message with a partner.
Your final submission should be:
1. Your source code
2. A description using English prose of your exchange with your partner. Say who your partner was, what
the message was, and how you gave them the key.
Assuming you send your key securely, congratulations, you now have a perfectly secret cryptosystem which
is impossible to break (mathematically). Remember not to reuse your key.