170 likes | 392 Vues
Javascript, Loops, and Encryption. October 24, 2008. Today. Reminder of what a for-loop looks like Crytoquotes The ASCII Table Converting characters to and from ASCII Programming encryption. For-loops. For loops in javaScript allow for the repetition of blocks of code
E N D
Javascript, Loops, and Encryption October 24, 2008
Today • Reminder of what a for-loop looks like • Crytoquotes • The ASCII Table • Converting characters to and from ASCII • Programming encryption
For-loops • For loops in javaScript allow for the repetition of blocks of code for (j = 1; j <= 10; j = j + 1) { document.write(“Hello\n”); } http://people.uncw.edu/guinnc/courses/Fall08/110/notes/day24_javascript7/hello.htm
Syntax of for-loops for ( initialization; continuation; next_iteration) { list_of_statements } • Initialization: Set the iteration variable to its starting value x = 0; j = 1;
For-Loops: Continuation for ( initialization; continuation; next_iteration) { list_of_statements } • Continuation: Test condition to see whether to execute the body of the loop. • The test condition is a boolean expression • If it is true, execute the list of statements • Otherwise, go to the next line after the entire for-loop. J < 20 Total <= N.
For loops: Next Iteration for ( initialization; continuation; next_iteration) { list_of_statements } • Next_iteration: Statements that are executed AFTER the list_of_statements are executed. • Good programming practice: next_iteration should change the value of the iteration variable. j = j + 1 x++
Cryptoquotes • A coded message where each letter has a substitute. • FRA PIF XD FRA LIF • THE CAT IN THE HAT
ASCII The American Standard Code for Information Interchange (ASCII)* is a standardized system for encoding characters numerically. It has several categories of characters: • letters: • upper case ('A' = 65 through 'Z' = 90); • lower case ('a' = 97 through 'z' = 122); • digits ('0' = 48 through '9' = 57); • punctuation • space = 32 through slash = 47; • colon = 58 through at sign = 64; • open square bracket = 91 through backquote = 96; • open curly brace = 123 through tilde = 126; • control characters, encoded as 0 through 31; also DEL (encoded as 127). *http://www.asciitable.com/
Converting to and from ASCII in Javascript • To find out the particular ASCII code of the first character in a string, s, var code = s.charCodeAt(0); • To convert a number, code, to its ASCII-mapped character, var s = String.fromCharCode(code);
Doing some encryption and decription • Let’s start with this file: Encrypt.htm • Some steps: • When the user hits “Encode”, put the plain text into the “Encoded” box. • Now, when the user hits “Encode”, put the ASCII values of the plain text into Encoded. • Now, add 1 to each of the ASCII values. • Convert each modified ASCII value to its appropriate character and put the result in Encoded.
Now let’s go the other way • Take the string in Encoded and decode it by subtracting 1 from each ASCII value.
Could we use a number besides 1? • Try it. • How can we make the code better? • Even better?
Next Time • How can we have the computer solve a cryptoquote where it doesn’t know the key?