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)
.
key = k0 k1 k2 ... kn-1
(where n is the length of the key) and plaintext message
ptxt = p0 p1 ... pm-1
, compute the ciphertext message
ctxt = c0 c1 ... cm-1
.
We encrypt one 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 ccNote: if any character in the key or the plaintext message is outside of ASCII 42-122 it is an ERROR!
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 one 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 pcNote: if any character in the key or the ciphertext message is outside of ASCII 42-122 it is an ERROR!