## Background

The Vigenere Cipher is a bit more complicated that the Caesar
Cipher (and a bit more secure).
It's still do-able by hand, though with more work.
The fundamental
operation is, like the Caesar Cipher, to "shift" a letter by
some fixed shift-value, but in the Vigenere Cipher each symbol
in the key is used as a different shift. If the key is as long as
or longer than the plaintext message, each character of the
plaintext message gets "caesar shifted" by a different value.
Otherwise, we repeat the key over and over until we get
something as long as the plaintext message.
See the

si110
symmetric encryption notes for more general-purpose
information.

For our purposes, all symbols - whether in the key, plaintext or
ciphertext - are characters in the ASCII range 42-122
inclusive.
We will represent a shift value by a single character in
that same range: 42 is a shift of zero, 43 is a shift of 1,
and so on.
In the following, we will go back and forth between
treating a character as an integer (via ASCII) and integer as a
character. In code, you must make explicit casts, like
`(int)c - 42`

or `(char)(i + 42)`

.

## Encrypting a plaintext message based on a key

Given key

`key = k0 k1 k2 ... kn-1`

and plaintext message

`ptxt = p0 p1 ... pm-1`

, compute the ciphertext message

`ctxt = c0 c1 ... cm-1`

.

We encrypt a character at a time:
to encrypt the index i character of the plaintext do the
following:

let pc be the index i character of ptxt
let sc be the index i mod n character of key
k = sc - 42
p = pc - 42
c = (p + k) mod 81
cc = 42 + c
define the index i character of ctxt to be cc

**Note:** if any character in the key or the plaintext message is outside of ASCII 42-122 it is an ERROR!

## Decrypting a ciphertext message based on a key

Given key

`key = k0 k1 k2 ... kn-1`

and ciphertext message

`ctxt = c0 c1 ... cm-1`

, compute the plaintext message

`ptxt = p0 p1 ... pm-1`

.

We decrypt a character at a time:
to decrypt the index i character of the ciphertext do the
following:

let cc be the index i character of ctxt
let sc be the index i mod n character of key
k = sc - 42
c = cc - 42
p = (c + (81 - k)) mod 81
pc = 42 + p
define the index i character of ptxt to be pc

**Note:** if any character in the key or the ciphertext message is outside of ASCII 42-122 it is an ERROR!