So I have this younger cousin. He's really into fooling around with codes/ciphers and such. I was into the same stuff as a kid, so I figure I'd save him all the work of encoding and decoding by hand, and write him a coupla cool little programs that would do it for him.
So there's a code table I'd like to generate for him, but I'm not sure of the best way to store the data for easy retrieval later. The table works like this:
- You take a key word, having as many non-repeating letters as possible. We'll use 'exchampion' for our example.
- You write out that keyword, followed by the remaining letters in the alphabet in order, using columns of 10 letters, like so:
e x c h a m p i o n
b d f g j k l q r s
t u v w y z . ! ? ,
The last 4 chars are just here to complete the table.
- Then, rearrange the columns in alpha order according to the topmost letter, and number them:
1 2 3 4 5 6 7 8 9 0
a c e h i m n o p x
j f b g q k s r l d
y w t w ! z , ? . u
- Finally, number the rows from 1 to 9 vertically, 3 numbers on each row, like so:
1 2 3 4 5 6 7 8 9 0
1,4,7 | a c e h i m n o p x
2,5,8 | j f b g q k s r l d
3,6,9 | y w t w ! z , ? . u
Now our table is complete. The way to use it is...you find the plaintext letter you want to encrypt, the ciphertext then becomes a two number set made of a randomly selected row number for that row, and the column number. So, plaintext 'a' becomes ciphertext 11, 41 or 71, 'b' becomes 23, 52, or 83, etc, etc.
I can figure out ways to generate the table, given a user supplied keyword, but I'm wondering what the best way to store it so it can easily be looked up against? My initial impression is to use 2 hashes, one with the plaintext as keys, the values of which are an array containing each of the 3 possible ciphertext values, and the other being the opposite, but is there a more elegant solution? Some way to store a matrix of data like that that I'm not thinking of?