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?
Thanks!
HaB
In reply to Kiddie Codes
by HaB
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.