Project: XOR Cipher
[[XOR_cipher|XOR Cipher]] is a particular easy type of stream cipher to use in encrypting data. It is a form of symmetric encryption in that the same password is used for encryption and decryption.
Write a program that reads in a file and writes out an encrypted form of the file to another file. It turns out that because XOR is its own inverse, the same algorithm with the same password will decrypt the file so you only need to run the program again on the encrypted file to decrypt it.
To encrypt or decrypt the file simply read a character from a binary mode file, XOR it with the corresponding character in the password, then write the result out to a binary file. Once you have used all the characters in the password, start again at the beginning of the password.
The program should take three arguments on the command line: name of input file, name of output file, and the password to use.
For example. Suppose you had a cleartext file called cleartext.txt that contained the following paragraph.
Orson Welles (May 6, 1915 - October 10, 1985) was an American actor, director, screenwriter and producer who is remembered for his innovative work in radio, theatre and film.
Using the Linux hexdump command we can see the values of the bytes in the file in both hexadecimal notation and as characters on the right.
After running our program to generate the ciphertext.bin file using the password ‘Rosebud’ we would see the following scrambled contents.
Here are the commands to encrypt, decrypt and compare the final results:
The diff command shouldn’t print any differences. On Windows use the “comp” command instead of “diff”.
Even though this can be a "perfect" encryption technique if the password is as long as the cleartext file, it doesn't always result in an unbreakable encryption. Why is this? What is the worst case?