(cryptography)=
# Cryptography

:::{commons-figure} https://commons.wikimedia.org/wiki/File:Confederate_cipher_disk.png
:align: right
:figwidth: 45%
A reproduction of the Confederacy's cipher disk
:::

You will implement two cryptosystems:

- [Caesar cipher](https://en.wikipedia.org/wiki/Caesar_cipher)
- [Vigenère cipher](https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher)

## Example run

```{command-output} code-prj/crypto/main
```

```{command-output} code-prj/crypto/main e caesar LEVERPOSTEJ
```
## Caesar

This cipher shifts each character in a plaintext by three letters forward with a wraparound in the end:

:::{table}

|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:

```c
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`:

```text
Plaintext:		ATTACKATDAWN
Key:			LEMONLEMONLE
Ciphertext:	 	LXFOPVEFRNHR
```

- The first character `A` encrypted with `L` gives `L`, because `A + L = 0 + 11 = 11 -> L`.
- The third character of the ciphertext is `F` because `T + M = 19 + 12 = 31 -> 5 -> F`

Implement the functions:
```c
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

<!--
Only uppercase characters must be processed – other characters are output as they are.
if we want this, then this makes the problem more difficult in the mapping part.
-->

## Testing

You can use [these tables](https://github.com/stanfordpython/python-assignments/tree/9fd0a2ed8795e4adf5bf19e6f8d4f9228b6ba0a7/assign1/tests) for testing.

## Warm-up activities

:::{activity} Flowchart command-line interface
Draw a flowchart for the program. Focus on the command-line interface (CLI).
:::

:::{card} 🤔 Question to ponder
Which C syntax features or statements would you use to implement the flowchart?
:::

## Credits

I copied and modified the [assignment materials](https://github.com/stanfordpython/python-assignments/tree/9fd0a2ed8795e4adf5bf19e6f8d4f9228b6ba0a7/assign1) by Parth Sarin, Michael Cooper and Sam Redmond.