Cryptography#
Fig. 15 A reproduction of the Confederacy’s cipher disk
CC BY-SA 3.0. By RadioFan (talk). Source: Wikimedia Commons#
You will implement two cryptosystems:
Example run#
$ code-prj/crypto/main
Usage:
code-prj/crypto/main MODE ALGORITHM TEXT [KEY]
Modes:
e, encrypt Encrypt the input text
d, decrypt Decrypt the input text
Algorithms:
c, caesar Caesar cipher
v, vigenere Vigenère cipher
Examples:
code-prj/crypto/main e c HELLO
code-prj/crypto/main decrypt v LQVRRBOFTQJ UGLE
$ code-prj/crypto/main e caesar LEVERPOSTEJ
OHYHUSRVWHM
Caesar#
This cipher shifts each character in a plaintext by three letters forward with a wraparound in the end:
plain |
A |
B |
C |
… |
Y |
Z |
|---|---|---|---|---|---|---|
cipher |
D |
E |
F |
… |
B |
C |
For example encrypting LEVERPOSTEJ gives
LEVERPOSTEJ
OHYHUSRVWHM
You have to implement two functions:
char *caesar_encrypt(char *plaintext);
char *caesar_decrypt(char *ciphertext);
These functions work in-place, in other words, you don’t have to create a new string for the encrypted/decrypted text.
Only alphabetic characters (A - Z) must be processed, others must be left as they are. You can assume that all characters will be uppercase.
Vigenère#
Similar to Caesar, however every character in the plaintext can be shifted by a variable amount. The amount to shift is determined by the key of alphabetic characters, where A corresponds to 0, B 1, etc. There is a wrap-around if necessary like in Caesar.
The key is repeated or truncated as necessary. For example if the key is LEMON:
Plaintext: ATTACKATDAWN
Key: LEMONLEMONLE
Ciphertext: LXFOPVEFRNHR
The first character
Aencrypted withLgivesL, becauseA + L = 0 + 11 = 11 -> L.The third character of the ciphertext is
FbecauseT + M = 19 + 12 = 31 -> 5 -> F
Implement the functions:
char *vigenere_encrypt(char *plaintext, char *key);
char *vigenere_decrypt(char *ciphertext, char *key);
You can assume that plaintext:
is uppercase
contains no spaces, numbers or punctuation
Testing#
You can use these tables for testing.
Warm-up activities#
Activity 32 (Flowchart command-line interface)
Draw a flowchart for the program. Focus on the command-line interface (CLI).
Which C syntax features or statements would you use to implement the flowchart?
Credits#
I copied and modified the assignment materials by Parth Sarin, Michael Cooper and Sam Redmond.