Homework, C Language

The goal of this homework is to provide three programs for dealing with cryptography. The programs you have to write will use Vigenère cypher to encrpyt files. You will provide programs for encrypting, and decrypting a file. The file will be encrypted with a secret passphrase known by only the two persons whishing to communicate. The students will moreover provide a program allowing a third party knowning both the clear text file and the encrypted version to discover the passphrase.
The students have to provide the following functionalities:
  • Encode a file using a passphrase.
  • Decode the file using a passphrase.
  • Use two files (clear-text + corresponding cyphered data) to discover the passphrase.
The students will encode and decode using the method of Vigenère (see Wikipedia page)

Vigenère cipher

The method consists for Alice to send a binary file to Bob (for instance an image, a program or a zip file). The file is encrypted using a passphrase that only Alice and Bob know.

The passphrase is composed of different letters. Suppose it is "Hello". The phrase is composed of different bytes 'H', 'e', 'l', 'l', and 'o'. The program will add (modulo 256) the letters of the passphrase to each of the bytes of the file. You will add 'H' (i.e 72) to the first byte, 'e' (i.e. 101) to the second, 'l' (i.e. 108) to the 3rd, 'l' (also 108) to the fourth, 'o'(=111) to the fifth and 'H' for the sixth, 'e' for the seventh, ...
To decrypt the file, you will subtract (also modulo 256) the value of the passphrase from the encrypted file to see the clear text data.
In the rest of the subject, the numbers are written in hexadecimal form
Example
Suppose the first bytes of the file are:

C5H, 00H, 3EH, 45H, 5EH, 89H, 01H, 90H
AAH, 65H, AEH, 9AH, FFH, 4FH, 13H, 08H
And the passphrase is "Hello" (or 48H, 65H, 6CH, 6CH, 6FH in hexadecimal).

To encrpyt the message, Alice needs to compute the following additions (modulo 256 or 100H):

(C5H+48H),(00H+65H), (3EH+6CH), (45H+6CH), (5EH+6FH), (89H+48H), (01H+65H), (90H+6CH) 
(AAH+6CH), (65H+6FH), (AEH+48H), (9AH+65H), (FFH+6CH), (4FH+6CH), (13H+6FH), (08H+48H) 
The encrypted message is :
0DH, 65H, AAH, B1H, CDH, D1H, 66H, FCH 
16H, D4H, F6H, FFH, 6BH, BBH, 82H, 50H 
Alice sends this encrypted message to Bob.

To decrypt, Bob will subtract each byte of the passphrase from the cyphered text. So he will do the following subtractions (modulo 256 or 100H).

(0DH-48H),(65H-65H), (AAH-6CH), (B1H-6CH), (CDH-6FH), (D1H-48H), (66H-65H), (FCH-6CH) 
(16H-6CH), (D4H-6FH), (F6H-48H), (FFH-65H), (6BH-6CH), (BBH-6CH), (82H-6FH), (50H-48H) 
And finally, Bob can decrypt the message
C5H, 00H, 3EH, 45H, 5EH, 89H, 01H, 90H  
AAH, 65H, AEH, 9AH, FFH, 4FH, 13H, 08H 

Cryptanalyse

If Charlie wants to intercept the communications between Alice and Bob, Charlie will want to discover the passphrase (and thus be able to decrypt all the possible messages). If Charlie can access two both a file in clear text and its encrypted version, this task is easy.
Charlie will do the subtraction of the clear text from the encrypted version. He obtains then the key repeated. He then just needs to find repetitions inside this key.
Charlie receives both files:
Clear text (Bytes written in hexadecimal)
 
C5, 00, 3E, 45, 5E, 89, 01, 90  
AA, 65, AE, 9A, FF, 4F, 13, 08 
Encrypted(Bytes written in hexadecimal)
0D,65, AA, B1, CD, D1, 66, FC 
16, D4, F6, FF, 6B, BB, 82, 50 
He does the subtraction (encrypted - clear text), still working modulo 256 / 100H.
(Bytes written in hexadecimal)
(0D-C5),(65-00), (AA-3E), (B1-45), (CD-5E), (D1-89), (66-01), (FC-90) 
(16-AA, (D4-65), (F6-AE), (FF-9A), (6B-FF), (BB-4F), (82-13), (50-08) 
He obtains the following result (Bytes written in hexadecimal):
48, 65, 6C, 6C, 6F, 48, 65, 6C
6C, 6F, 48, 65, 6C, 6C, 6F, 48
He finally needs to find the length of the passphrase. This is done by comparing the string starting at index 0 with the string starting at index n (n=1, 2, 3, 4, 5, 6, 7, 8, 9 ,10, ...).

To do

One program for encrypting and decrypting files

The students will write a C-program for encrypting and decrypting a file. The program is started with a file as an argument and -d for decrypting. The passphrase is read from the standard input. Examples:

$ vigenere image.png 
What is your passphrase? MyComplexPassphrase
The command produces the file image.png.encrypted, that is then sent to Bob.

Bob executes the following command (where -d means decrypt):

$ vigenere -d  image.png.encrypted
What is your passphrase? MyComplexPassphrase
Produces the file image.png.

The program is also used for finding the passphrase

The program you have to write finds the original passphrase knowning only one file and the corresponding encrypted file.
$ vigenere -hack image.png image.png.encrypted
The pass phrase used was: MyComplexPassphrase

Conditions

This exercise is to be done in a group of two students. If a class has an odd number of students, one student will work alone.
The program must be written in C language.
Deadline: 27th of January 2017, the students must send to the professor a zip file containing a directory with the C files and a makefile for compiling them. If you have written a library of procedures, you must also not forget this file. The program must work (compile and be executable) on the Linux system used for exercises in this course.