Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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 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?



    In reply to Kiddie Codes by HaB

    Use:  <p> text here (a paragraph) </p>
    and:  <code> code here </code>
    to format your post, it's "PerlMonks-approved HTML":

  • 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, 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, 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (1)
As of 2024-07-16 08:21 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.