1 / 17

Javascript, Loops, and Encryption

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

yardan
Télécharger la présentation

Javascript, Loops, and Encryption

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Javascript, Loops, and Encryption October 24, 2008

  2. Today • Reminder of what a for-loop looks like • Crytoquotes • The ASCII Table • Converting characters to and from ASCII • Programming encryption

  3. 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

  4. 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;

  5. 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.

  6. 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++

  7. Cryptoquotes • A coded message where each letter has a substitute. • FRA PIF XD FRA LIF • THE CAT IN THE HAT

  8. 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/

  9. ASCII Table #1

  10. ASCII Table #2

  11. ASCII Table #3

  12. ASCII Table #4

  13. 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);

  14. 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.

  15. Now let’s go the other way • Take the string in Encoded and decode it by subtracting 1 from each ASCII value.

  16. Could we use a number besides 1? • Try it. • How can we make the code better? • Even better?

  17. Next Time • How can we have the computer solve a cryptoquote where it doesn’t know the key?

More Related