University of Calgary

one time pad

Encryption in C
One Time Pad
source: Katrin Becker, 2002 

Assignment Links:

C & UNIX (lab)
(lab) Encryption
One Time Pad Encryption - 0.9.4. www.vidwest.com/otp/
One-Time Pad Generator www.fourmilab.ch/onetime/otpjs.html
One-Time Pad Generators www.fourmilab.ch/onetime/
Why Are One-Time Pads Perfectly Secure?
http://world.std.com/~franl/crypto/one-time-pad.html
ONE-TIME PAD CRYPTOGRAPHY www.contestcen.com/crypt005.htm
One-time pad - Wikipedia www.wikipedia.org/wiki/One-time_pad
One-Time-Pad Frequently Asked Questions www.ranum.com/pubs/otpfaq/


Note: I know that many solutions to this assignment exist (ALL over the place). I can guarantee you will learn more if you write your own.
Problem: Write an encryption program that reads from stdin and writes to a specified file using a One-TimePad encryption scheme.
Goals, Skills and Concepts:
- back to the procedural paradigm
- show some of the utility of working in a lower-level language
- interact with operating system
- deal with files as binary entities
- must be a problem 'best' solved NOT using OO
What is a One-Time Pad?
A one-time pad is a very simple yet completely unbreakable symmetric cipher. "Symmetric" means it uses the same key for encryption as for decryption. As with all symmetric ciphers, the sender must transmit the key to the recipient via some secure and tamperproof channel, otherwise the recipient won't be able to decrypt the ciphertext.
The key for a one-time pad cipher is a string of random bits, usually generated by a cryptographically strong pseudo-random number generator (CSPRNG). For more information, see David Deley's Computer Generated Random Numbers. It is better to generate the key using the natural randomness of quantum mechanical events (such as those detected by a Geiger counter), since quantum events are believed by many to be the only source of truly random information in the universe. One-time pads that use CSPRNGs are open to attacks which attempt to compute part or all of the key.

With a one-time pad, there are as many bits in the key as in the plaintext. This is the primary drawback of a one-time pad, but it is also the source of its perfect security (see below). It is essential that no portion of the key ever be reused for another encryption (hence the name "one-time pad"), otherwise cryptanalysis can break the cipher.

The cipher itself is exceedlingly simple. To encrypt plaintext, P, with a key, K, producing ciphertext, C, simply compute the bitwise exclusive-or of the key and the plaintext:

C = K^P
To decrypt ciphertext, C, the recipient computes
P = K^C
It's that simple, and it's perfectly secure, as long as the key is random and is not compromised.
If no key (or a null string) is passed, then no encryption is done.
 
NOTE: encrypted text will probably be UN-readable as text. In order to view the output in the file you must use the UNIX facility od (octal dump). ATTEMPTS TO DISPLAY THE OUTPUT FILE AT THE SCREEN MAY MESS UP YOUR SCREEN OR WORSE!!! ALSO: DO NOT ATTEMPT TO PRINT THE OUTPUT FILE (same reasons, plus it will annoy UCS and may result in many pages being printed which you may have to pay for).
 
Testing:
Test your program using at least the following:
source strings to encode:
1. There's no cheese Gromit!
2. The needs of the many outweigh the needs of the few, or the one.
3. All lies lead to the truth.
4. Fight the Future
5. Cracking toast, Gromit
6. ' ' (i.e. a single blank - must be in quotes)
 
Run at least 2 of them through the encoder twice to prove the de-cryption works.
C- Version
- basic OTP (one-time-pad); generates key as message is encripted
- output placed in 2 seperate files: OTP and OUT
- implements debug trace that can be turned on by an optional command line argument
- if a filename is provided in the command line, it is used as the pad for decryption
B- Version
- allow user to specify input (message) file name in command line argument
- generate output file name based on input file name
A- Version
- allow user to specify length of pad (which will be generated automatically and then re-used as often as necessary to encrypt the message.
 
BONUSES:
_______1. [up to 8 points ] allow encryption "in place" so the output file replaces the original input file
_______2. [[2 points] allow > 1 line of input (will need an end-of-input marker)
_______3. [up to 8 points ] allow the order of the arguments to vary (except the file names which must have source file name before the target file name and pad file name)
_______4. [2 points] allow optional arguments (if -d/-e omitted default to -e; if filenames omitted assume "IN", "OUT" "OTP")
_______5. [2 points] if the user types 'encrypt' with no arguments, print a unix-style man page



Updated: August 29, 2005 12:54 PM