Cryptography (Secret Codes)

Cryptography - Codes

Caeser Cypher 
    This is where you shift every letter over - a set number of places  

    a  b  c  d  e   f  g  h   i   j    k   l    m  n   o   p   q   r    s   t    u   v   w   x    y   z
    1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

    so if the number to shift is 3 then we shift every letter of the message down


    e.g.  If we want to send the message okay:

             o is letter #15     k is #11   a is #1    y is #25   

            We add 3 to every letter
                    o becomes letter #18  (15+3=18)   or "r"
                    k becomes letter #14  (11+3=14)   or "n"
                    a becomes letter #4  (1+3=4)  or "d"
                    y becomes letter #28 (25+3=28) but ther is no letter #28, 
                           so we wrap around (start from the beginning again)
                            (28-26=2)  or "b"
            So the encoded message is now "rndb"
       

    All programming languages have string functions to do this:
        if you have a letter, you can get its number  (often called the location or offset)
        if you have the number, you can get the letter (often called the substring, substr or char function)

Finding the offset (location) of the letter in the alphabet

    If you have a string with the letters of the alphabet - "abcdefghijklmnopqrstuvwxyz", you can find where a letter is in it with the "offset" function

    The offset function tells you the offset (or location) where the letter was found.
        
    if we 
        put "abcdefghijklomnopqrstuvwxyz" into str
   
             offset("c", str) will return to us a "3" because it found the "c" in location 3 in our string "str"
             offset("a", str) will return to us a "1" because it found it at location 1 in our string "str"
             offset("k", str) will return to us a "11" because it found it at location 11 in our string "str"

    if it doesn't find it, it will return a zero (0)

             offset("$", str) will return to us a "0" because it did not find a "$" in str


Finding the new Letter with a new offset number

        If we add a 3 to the letter, we can use that to get the new letter using the char # function.

    The char # gets the character at that location

        char 1 of str         gives us the "a"
        char 4 of str         gives us the "d"
        char 26 of str       gives us the "z"

Put that together and we can get the new code letter:

            put offset("k", str)  into x
            put x+3 into x
            put char x of str into newletter

    newletter should now have the letter "n" in it (letter "k" + 3)


Looping through the original message to get all the letters and convert them:

    put empty into codedMessage                         // clear out the coded message

    repeat for each char c in myMessage             // go thru every letter in your message
             put offset(c, str)  into x                            // get its offset
             put x+3 into x                                         // add 3 to it
             put char x of str after codedMessage    // get the new letter and add it to the end of the coded message
    end repeat

    note: the keyword "after" adds the letter to the end. if we used "into", it would wipe out the contents and replace it with the new letter



Extra Credit - Challenges:
    Once you get the program working, we have a few situations to contend with:

    1. What about letters that go off the end. For example the letter "y" is number 25. If you add 3 to it, you have 28. When you go to get char 28, you will get an error. It needs to wrap around to the start of the alphabet. (28-26 = 2 which should get letter "b" instead)

    2. What about spaces and punctuation? Those do not get coded. They should just get passed on.

    3. What about decoding a message? To decode, you need to subtract 3 from the codedMessage to get the original

    4. How could you make it so the user can pick different offsets - e.g. use 5 instead of 3 as the number. 
    
         
If you get stuck, as a Last Resort...

    You can download the program below to see how it is done in the LiveCode language. Look at the code on the "Encode" and "Decode" buttons
ċ
CaeserCypher.livecode
(6k)
cyril.pruszko@pgcps.org,
May 6, 2016, 10:42 AM
Comments