Creating Your Own
"Secret Message" Programs


So, you've downloaded and installed SiMPLE on your computer. And now you'd like to use it to encrypt and decrypt "secret messages", right? OK, let's do it! But first, you will need to become familiar with text strings and how to work with them.

NOTICE:


The following discussion assumes that you know how to
write at least elementary SiMPLE computer programs.

There! Now that we've scared away all the "newbies", let's begin!

The SiMPLE system library contains a number of very useful functions for working with text strings. Let's look at some of the functions that we will need to use:

Length

The "Length" function determines the number of characters in a string. For example, the following program:

           Set message = "I love SiMPLE!"
           Set long = Length (message)
           Display long
will display the number 14, because there are a total of 14 characters (letters, spaces, etc.) in the "message" string.

Ascii

Computers can work only with numbers (not letters, punctuation marks, or anything else). So, in order to deal with non-numerical data (such as text strings), some guys got together one day and arbitrarily decided to assign numerical values to all the possible text characters. They assigned the number 65 to the letter "A", the number 66 to the letter "B", and so on. They then called these numbers the ASCII values for each corresponding text character.

The "Ascii" function converts a text character into its corresponding ASCII (numerical) value. For example, the following program:

           Set letter = "D"
           Set number = Ascii (letter)
           Display number
will display the number 68.

Char

The "Char" function does the exact opposite of the "Ascii" function. "Char" converts an ASCII value into a text character. For example, the following program:

           Set number = 67
           Set letter = Char (number)
           Display letter
will display the letter "C".

Extract

The "Extract" function is used to "copy out" individual characters (or substrings) from a given text string. The "Extract" function requires three calling parameters:

  1. The text string from which characters are to be extracted
  2. The location of the first character to be extracted from the string
  3. The location of the last character to be extracted from the string

IMPORTANT: The very first character of a string is in location ZERO (not location ONE).

For example, the following program:

           Set message = "I love SiMPLE!"
           Set substring = Extract (message, 3, 9)
           Display substring
will display characters 3 through 9: "ove SiM"

In addition to the four text functions discussed above, you will also find the library's "Cycle" function to be quite useful:

Cycle

The "Cycle" function is just like the "%" operator (i.e., "modulo"), except that it works properly for negative numbers as well as for positive numbers. For example, the following program:

           Do n = 7 To -8
              c = Cycle (n, 4)
              Display c,,
           Loop
will display: 3 2 1 0 3 2 1 0 3 2 1 0 3 2 1 0

We are now ready to start doing some serious encrypting!

One very easy encryption method is to merely shift each letter of the message three letters down in the alphabet. This is known as the "Caesar Shift" method, because Julius Caesar used it in some of his writings. (And he didn't even have a computer to help him do the work!) But how do we tell the computer to shift a letter (such as "M") three places? (We can't just write: "M"+3)

To shift a letter three places, all we have to do is first convert it into a number in the range 0-25 (where "A" is 0, "B" is 1, "C" is 2, etc.), then add three to that number (modulo 26, of course), and finally convert it back into a letter. For example, the following program would shift the letter "M" three places:

   Set letter = "M"                 //(the letter to be shifted)
   Set delta = 3                    //(the distance to be shifted)
   Set asc = Ascii (letter)         //Convert to ASCII number
   Set val = asc - Ascii ("A")      //Move into 0-25 range
   Set val = Cycle (val+delta, 26)  //NOW we shift it!
   Set asc = val + Ascii ("A")      //Convert back to ASCII range
   Set letter = Char (asc)          //Convert to text character
   Display letter                   //Display the shifted letter ("P")
(If you had wanted to, the above operations could have all been written as a single statement:
         Display Char(Cycle(Ascii("M")-Ascii("A")+3,26)+Ascii("A"))

but then the individual steps of the process might not have been as easy to see.)

Let's now modify our previous program to encrypt not just a single letter but a complete message. We will create a loop in which each letter of the message is extracted (one at a time) and then shifted into a new letter:

   Set message = "MEET ME IN THE FOREST TONIGHT AT MIDNIGHT"
   Do n = 0, Length (message) - 1
      Set letter = Extract (message, n, n)  //Get the letter to be shifted
      If letter != " "                      //Don't shift spaces!
         Set delta = 3                      //(the distance to be shifted)
         Set asc = Ascii (letter)           //Convert to ASCII number
         Set val = asc - Ascii ("A")        //Move into 0-25 range
         Set val = Cycle (val+delta, 26)    //NOW we shift it!
         Set asc = val + Ascii ("A")        //Convert back to ASCII range
         Set letter = Char (asc)            //Convert to text character
      Endif
      Display letter,                       //Display the character
   Loop n
When you run the program it will display:

PHHW PH LQ WKH IRUHVW WRQLJKW DW PLGQLJKW

To decrypt the message, all you have to do is subtract three from each letter in the encrypted message:

   Set message = "PHHW PH LQ WKH IRUHVW WRQLJKW DW PLGQLJKW"
   Do n = 0, Length (message) - 1
      Set letter = Extract (message, n, n)  //Get the letter to be shifted
      If letter != " "                      //Don't shift spaces!
         Set delta = 3                      //(the distance to be shifted)
         Set asc = Ascii (letter)           //Convert to ASCII number
         Set val = asc - Ascii ("A")        //Move into 0-25 range
         Set val = Cycle (val-delta, 26)    //Shift it BACK!
         Set asc = val + Ascii ("A")        //Convert back to ASCII range
         Set letter = Char (asc)            //Convert to text character
      Endif
      Display letter,                       //Display the character
   Loop n
When you run the program it will display:

MEET ME IN THE FOREST TONIGHT AT MIDNIGHT


Conclusion

Now that you know how to write your own encryption and decryption programs, start becoming creative! There's no need to limit yourself to simply shifting each letter by the same fixed amount (like in our Caesar Shift example where delta always had the same constant value of three). Try shifting each letter by a different amount. For example, you might want to increment the value of delta as each new character of the message is extracted. Or you might want to use the random number generator to specify the values for delta. The more complicated you make things, the harder it will be for anyone to break your codes!



  [ Webmaster | FAQ's | Home Page | Contact Us ]