# LetterPad encryption

© Francisco Ruiz, 2015

This page implements the paper-and-pencil "LetterPad" cipher by F. Ruiz, similar to DicePad but operating directly with letters, for those who wish to use a computer as a convenience. All steps can be performed by hand without excessive effort. The process is described in detail in this article.

LetterPad uses normal text taken from a book or similar source to generate a base24 one-time pad of sorts, which is then used to encrypt a message. Then the plaintext is subtracted from the base24 keystream to produce the ciphertext, using a Tabula Recta. Because the number of symbols must be 4! = 24, the letters J and Q are omitted and replaced by I and K, respectively. Since common text has an average entropy of 1.58 bits per character and the keystream-producing method uses four pieces of text as long as the plaintext, the key text has an entropy of 1.58 x 4 = 6.32 bits/plaintext character, which is greater than the 4.585 bits/character of pure random letters (base24), thus potentially producing a perfectly random keystream, which ensures a perfect cipher per Shannon's criterion.

The process is identical for encryption and decryption. After the plaintext or ciphertext is processed —spaces and punctuation are stripped, and all letters are converted to capitals; accented letters are replaced by their non-accented versions; J's turn into I's and Q's into K's, numbers in plaintext are converted to letters as in 0=A,1=B,...9=K, but are not converted back— a piece of similarly processed key text (except that digits and J Q are kept) is selected and used to generate a random keystream made of letters. Finally the plaintext (ciphertext, when decrypting) is subtracted letter by letter from the keystream (and substituted, if the alphabet is not the straight one) by means of a Tabula Recta. Using an alphabet derived from the key text makes the final ciphertext non-malleable.

### Tabula Recta

`      A B C D E F G H I K L M N O P R S T U V W X Y Z  `
```    -----------------------------------------------
0: a | A B C D E F G H I K L M N O P R S T U V W X Y Z | a
1: b | B C D E F G H I K L M N O P R S T U V W X Y Z A | b
2: c | C D E F G H I K L M N O P R S T U V W X Y Z A B | c
3: d | D E F G H I K L M N O P R S T U V W X Y Z A B C | d
4: e | E F G H I K L M N O P R S T U V W X Y Z A B C D | e
5: f | F G H I K L M N O P R S T U V W X Y Z A B C D E | f
6: g | G H I K L M N O P R S T U V W X Y Z A B C D E F | g
7: h | H I K L M N O P R S T U V W X Y Z A B C D E F G | h
8: i | I K L M N O P R S T U V W X Y Z A B C D E F G H | i
9: k | K L M N O P R S T U V W X Y Z A B C D E F G H I | k
10: l | L M N O P R S T U V W X Y Z A B C D E F G H I K | l
11: m | M N O P R S T U V W X Y Z A B C D E F G H I K L | m
12: m | N O P R S T U V W X Y Z A B C D E F G H I K L M | n
13: o | O P R S T U V W X Y Z A B C D E F G H I K L M N | o
14: p | P R S T U V W X Y Z A B C D E F G H I K L M N O | p
15: r | R S T U V W X Y Z A B C D E F G H I K L M N O P | r
16: s | S T U V W X Y Z A B C D E F G H I K L M N O P R | s
17: t | T U V W X Y Z A B C D E F G H I K L M N O P R S | t
18: u | U V W X Y Z A B C D E F G H I K L M N O P R S T | u
19: v | V W X Y Z A B C D E F G H I K L M N O P R S T U | v
20: w | W X Y Z A B C D E F G H I K L M N O P R S T U V | w
21: x | X Y Z A B C D E F G H I K L M N O P R S T U V W | x
22: y | Y Z A B C D E F G H I K L M N O P R S T U V W X | y
23: z | Z A B C D E F G H I K L M N O P R S T U V W X Y | z```

To subtract two letters: find the second letter on the top and then go down that column until the first letter is found; then follow that row all the way to the left or to the right to find the result at the edge. Example with straight alphabet: R - H = I (first find H at the top, then go down until R is found, then go left or right to find I).

## Step 1. Tabula preparation

We begin by making a scrambled alphabet out of the key text, which is placed at the top and the sides of the Tabula Recta. This is why we input the key text before the plain text. The box is blue to indicate that it can be written on. Do this to make a scrambled alphabet: 1, take the first sentence in the key text (up to the first period) and write down new letters in the order they appear; if a letter in the key text has already been written, write instead the first letter before it in the alphabet that is still available (wrap around to the end if needed); 2, then write the rest of the alphabet in reverse order.

When a plaintext is written, the key text is processed in the same way as the plaintext (capitals rather than lowercase) and a piece of it one letter longer than the processed plaintext is taken and placed in the seed box. If it is too short, it will be repeated and a warning will appear below the key text. This key text will be used to encrypt the plaintext (or decrypt the ciphertext), and then it will never be used again.

### Key Text

This is where the warning will appear

Let us select whether the alphabet that forms the basis of the Tabula Recta will be scrambled according to the key text, or the straight alphabet will be used: Warning: encrypted messages using default encoding can be altered by an adversary, plus the key text can be recovered if the plaintext is known.

Key text-derived alphabet     Straight alphabet

There is also the possibility of running a lagged Fibonacci generator (LFG) right after the keystream is generated in order to achieve better randomness when the plaintext is very long. This is selected here:

No LFG     Use LFG

## Step 2. Plaintext encoding / Ciphertext preparation

Now we write the plaintext that we wish to encrypt, which will be converted to lowercase. Punctuation and spaces will be removed. Diacritical marks (accents) will be ignored.

### Plaintext / Ciphertext

Which is converted to this, after spaces, punctuation and diacritics are removed, and everything is turned into lowercase:

## Step 3. Keystream generation

The next step is to generate the keystream. We begin by stripping spaces, accents, and punctuation from the key text (but leaving numerals on) and taking a sufficient number of blocks of it of a certain length, calculated below. We need a minimum of four characters per encoded plaintext letter. Since the keystream-generating process produces about 1/3 rejects, we take a total amount of key text that is a 6 times the length of the plaintext rather than just 4 times. If the key text is not long enough, it is repeated and a warning is shown below.

### Processed Key Text

The blocks are going to be split into four parts, which we will write into four separate lines below, so the block length must be a multiple of 4. If shuffling is used, each line must be a mulltiple of the shuffling period applied, so the line length is the least common multiple of the shuffling periods, entered below. The first row contains the first part of each block, the second row the second part, and so on. Blocks are arranged horizontally, separated by spaces:

### Key Text set up as four rows, unshuffled

Now the letters (and any digits) in each part may be shuffled by leaving a constant number of spaces (period -1) between letters as we begin to write each part, continuing at the first available space in that part every time the end is reached. Example with period 3 shuffling: "ABCDEFGHIJKL" becomes "AEIBBJCGKDHL". It is best to use a different period for each part, so the total length of each part is the least common multiple of the three periods. If all the periods are the same, there is no shuffling. Default periods are 10,10,10,10, which lead to parts 10 characters long, for a total of 40 characters per block and no shuffling. This can be changed in the boxes below.

### Shuffling Periods

Part 1:    Part 2:    Part 3:    Part 4:

With these values, each block comprises this many characters:

And this is the result after shuffling:

### Shuffled Key Text

And now we are going to calculate a letter order (0 to 23) from each column, which is then converted to a letter using the pattern on the left side of the Tabula Recta. Start with 0 value and then do this:

1. Add 6 if the character on the second row follows the character on the first row in natural alphabetical order (digits precede all letters), then add 6 if the character on the third row follows that on the first, and another 6 if the character on the fourth row follows that on the first.
2. Add 2 if the character on the third row follows that on the second, and another 2 if the character on the fourth follows that on the second.
3. Add 1 if the character on the fourth row follows that on the third.
4. Skip the column and go to the next if any two characters are the same.

The result, converted into letters as on the left side of the Tabula Recta, is the raw keystream, where dots mark the skips:

### Raw Keystream

If a LFG is to be used, we apply it now. Take the last letter of the raw keystream and write it below the first. Then subtract the first from the second using the Tabula Recta, and write the result below the second letter. Then continue doing the same until the second row is filled. That second row is the LFG-processed keystream (will be the same as the raw keystream if no LFG is used). Then randomness of the final keystream is measured by different tests, and the result is displayed below.

### Final Keystream

Information about keystream quality will appear here

## Step 4. Encrypted Ciphertext / Decrypted Plaintext

Finally, we subtract the processed plaintext (processed ciphertext, when decrypting) from the keystream using the Tabula Recta, resulting in the raw ciphertext below (plaintext, when decrypting), which is ready to be sent out. Because this is output, the cells are shaded green.