Project: ChaCha20 Cipher
[[Salsa20|ChaCha20]] is a particular efficient 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 ChaCha20 keyblock, then write the result out to a binary file. Once you have used all the characters in the keyblock, generate the next keyblock using the ChaCha20 algorithm.
The program should take three arguments on the command line: name of input file, name of output file, and the password to use. Duplicate the password enough times to get the 32 bytes needed for the ChaCha20 key. Padd the password with "1234567" and then truncate to 8 bytes and use as the nonce. Usually the nonce will be set to a random value when used as a communications stream cipher.
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 an efficient encryption technique what are the chances that it can be deciphered? Why is this? What is the worst case?
See [[ChaCha20 Cipher Reference]] for a reference implementation.