## Wednesday, January 22, 2014

### Caesar Cipher

A Caesar cipher is a very simple encryption technique where each letter is shifted a fixed number of characters in the alphabet. It is named after Julius Caesar, who apparently used this technique in some of his letters.

For example, if we were to encode `"FACTOR"` by shifting each character to the right by three letters, we would get `"IDFWRU"`. The `"F"` shifts to `"I"`, the `"A"` shifts to `"D"`, the `"C"` shifts to `"F"`, etc.

Let's implement this in Factor!

First, we implement a word to shift a character (uppercase by convention) a specified number of letters. Using `"A"` as our "zero" point by subtracting, shifting modulo 26 character ascii alphabet, then re-adding the ascii value for `"A"` (65):

```: caesar-shift ( ch n -- ch' )
[ CHAR: A - ] dip + 26 rem CHAR: A + ;```

Next, a word for shifting every letter in a string (preserving numbers and punctuation):

```: caesar-map ( str n -- str' )
'[ dup CHAR: A CHAR: Z between? [ _ caesar-shift ] when ] map ;```

This lets us implement the encrypt and decrypt words. Encrypting is simple and decrypting is mapping with a negative shift number:

```: caesar-encrypt ( plain n -- encrypted ) caesar-map ;

: caesar-decrypt ( encrypted n -- plain ) neg caesar-map ;```

Trying it out:

```IN: scratchpad "HELLO, WORLD!" 3 caesar-encrypt .
"KHOOR, ZRUOG!"

IN: scratchpad "KHOOR, ZRUOG!" 3 caesar-decrypt .
"HELLO, WORLD!"```