Oct. 14 - Oct. 16
EncryptStep 1: Iterate over the secret messageIn security terms, we call the secret message the "clear text", because it is not encrypted. Your function receives an object called data, that contains the information from the HTML page. It has the following properties:
data.cleartext holds a String.Step 2: Get the ASCII code for each character in the messageWe have not discussed ASCII representation yet, but it is really simple, so don't worry. Basically it is a way for the computer to think of every letter (and other characters) as a number. For this lab we only use lower case letters, so we only use ASCII values of 97 (a) through 122 (z). Check out the table on the right! You can get this value using the charCodeAt() function like so data.cleartext.charCodeAt(position). Note that since we are passing the character's position to a function, it is in parentheses (), not index brackets [].
Step 3: Apply the Substitutiondata.key holds the numerical value by which you have to shift your characters. Let's say your first letter is an m. In step 2 you got the ASCII value of 109. Now you just need to add data.key to that value. If data.key is 4, your new value should be 109+4=113. This is perfect. But what happens if data.key is 20? Then 109+20=129, but our alphabet ends with the z at position 122. Well, do as Caesar did. Whenever your newly computed value exceeds 122, just subtract 26 to jump to the beginning of the alphabet. 129-26=103. Perfect, that'll work!
Step 4: Find out what letter your new ASCII value represents.We have provided a function calledmakeCharacter for you that uses Javascript's powerful String object to resolve ASCII values to characters. You can call makeCharacter(103) and it will return "g".
Step 5: Build the Cipher TextWe like to call an encrypted message a "cipher text". As you go through the letters of the clear text and substitute them with another one, append ("add") each substitute to theciphertext variable. You will notice that this variable gets returned at the end of the encrypt function, so this is where you need to store your encrypted message.
| ![]() |
Now that you have this part done, the decrypt function won't take much time at all! Let's finish this lab!