Math and
All Math
How Math
Helped Me
Lesson 1  Lesson 2  Lesson 3  Lesson 4  Lesson 5  Lesson 6  Lesson 7

Lesson 4 - Improved 2-Letter Encoding Program

Note: To use the Javascript programs in these lessons, you will need to use a FireFox browser or other non-Internet Explorer browser due to the "feature" in the IE browser disallowing Javascript prompts.

This program does the following:

  1. Prompts you to input a letter. Then outputs the letter (to your screen). This is done for 8 letters.
  2. Assigns a variable to your letter.  For example, the first letter is assigned to the variable letter1a.
  3. Assigns each variable a value of 1 through 26, depending on what letter it equals where A = 1, B = 2, .....Y = 25, Z = 26.  So if letter1a = "C", then letter1a = 3.  If you enter something other than a letter A-through-Z or you enter nothing at all and hit the return, the variable is assigned to the value " * "
  4. Add 2 to each letter variable value using the formulas letter1a = letter1a +2, letter2b = letter1b +2, letter3c = letter3c +2, etc..  So if letter1a=3, 2 is added and letter1a becomes equal to 5. The program "adds" 2 to * by calling it *2.  Also, apply modular arithmetic in base 26 to this sum so that any sum over 26 will be assigned to the remainder after dividing by 26. For the case of *2 corresponding to a space, an undefined value is calculated, which is not a problem since it ultimately gives you " * ".

    Note: Javascript uses the % symbol to invoke the modulus operations.  So the formula letter1a = (letter1a + 2)(Modulus 26) is coded as letter1a = (letter1a + 2)%26
  5. Assigns each new value back to a letter to produce a coded message.  So if letter1a is 5, letter1a is assigned the value becomes "E". Then outputs the letter. This is done for 8 letters. Note that if the letter variable was assigned a value of " *2 " or the new value is something other than a number from 1-to-26, it will output the value " * ". 
  6. One additional change: Since the letter "X" is assigned to the value 24 and 24+2 = 26, the resulting remainder upon dividing 26 by 26 is zero.  So I changed the code so any character with value ZERO is assigned to the letter "Z"

To see the code used in this program, select View, Page Source in your browser's menu.

We Try Encoding "ZIG ZAG" Again
This time, the values of 25 and 26  become 27 and 28 respectively when 2 is added but the modulus-26 operation changes 27 to 1 and changes 28 to 2. A new problem occurred with the letter "X", since this was assigned a new value of zero, since 26 divided by 26 has zero remainder. This was fixed by letting the value zero correspond to the letter Z in the output.  Our new result is shown at the bottom of this page. Right as rain!

Why Is This Such an Easy Code To Decipher?
One need only print out two copies of the alphabet and lay them next to each other as shown here:
There is a much better code that can not be broken so easily. Check out this BETTER SECRET CODE here.









Copyright 2012, Michael Sakowski and His Licensors
News on My Blog! Share Your Ideas! Copyright Info Suggested Uses Links Contact FAQs