8.12

### Lab 7: Secret messagesðŸ”—

Note: Whenever you design or write a function, you need to follow the design recipe.

#### 1Reading secret messagesðŸ”—

One important topic studied in computer science is how to send secret messages without being found out. For example, a voter should be able to send a secret vote to a counting machine. To make sure nobody else can read the secret message, we send a different, corresponding message that nobody else can read. This is called encrypting the secret message. Recovering the secret message from the encrypted message is called decrypting it.

A basic method to encrypt a secret message is to replace some letters with other letters. This method is called a substitution cipher. ROT13 (“rotate by 13 places”) is a simple substitution cipher that replaces a letter with the 13th letter away from it in the alphabet.

So, for example, "a" would be replaced with "n", "O" would be replaced with "B", and so on. We can write secret messages using this cipher, and will be decrypting some secret messages in this lab.

Exercise 1. Here we provide a data definition for a 1String, and a definition for rot13. Given a 1String, rot13 checks if it is a lowercase letter between "a" and "n", if so, it will add 13 to the integer representation of the 1String, which results in the 13th letter after it in the alphabet. Similarly, rot13 checks if is a lowercase letter between "n" and "z", and subtracts 13 to get the 13th letter before it in the alphabet. rot13 does the same thing for uppercase letters. The else case handles 1Strings like " " and ".", which don’t rotate.
 ; A 1String is a String of length 1 ; rot13 : 1String -> 1String ; returns the letter 13 spaces ahead in the alphabet (define (rot13 letter) (cond [(or (and (string<=? "a" letter) (string<=? letter "m")) (and (string<=? "A" letter) (string<=? letter "M"))) (int->string (+ (string->int letter) 13))] [(or (and (string<=? "n" letter) (string<=? letter "z")) (and (string<=? "N" letter) (string<=? letter "Z"))) (int->string (- (string->int letter) 13))] [else letter]))

Write tests for the rot13 function. Make sure to test every possible case of the cond clause.

Exercise 2. Recall data definitions for lists from class. Write down the data definition for a ListOf1Strings. Define three examples of it. Write down the template for a function that processes a ListOf1Strings; make it look like a function called process-1strings, and do not put it in a comment.

Exercise 3. Design a function combine-1strings which takes a ListOf1Strings and returns it as a single String. combine-1strings should return "" if the ListOf1Strings is empty.

Exercise 4. Download the following five text files into the same folder as the file where you are completing this assignment:
To download these files, don’t use “Save Page As” or “Save As”; use “Save Link As” or “Download Linked File” in your Web browser. If you can’t find the command, try right-clicking or two-finger-tapping or long-pressing.

Go ahead and try to read the files. Looks like a bunch of gibberish! Let’s get to cracking these messages.

Exercise 5. Design a function convert-to-r13 which takes a ListOf1Strings and returns a ListOf1Strings with each 1String in the list converted to its rotated representation.

Exercise 6. In the previous exercise, you should have written at least one function example where the input to convert-to-r13 is a ListOf1Strings with some 1Strings. Calculate step-by-step from this example.

But when you get to the point where rot13 is called, step directly to what the result of the call should be. And when you get to the point where convert-to-r13 is recursively called, step directly to what the result of the call should be.

Exercise 7. Design a function file-to-r13 which takes a String (a file name) and returns the file as a ListOf1Strings with each 1String converted to its rotated representation.

You will need to use the 2htdp/batch-io library to examine the contents of files. You should use its read-1strings function to create a ListOf1Strings from a text file.

Use the files you downloaded as tests. They should start looking less like gibberish!

Exercise 8. Finally, put it all together. Design a function decrypt-file which takes a String (a file name) and returns the contents as a single String converted to its rotated representation. Remember combine-1strings from Exercise 3.

Now you should be able to read the secret messages! Terng wbo!

#### 2Writing our own secret messagesðŸ”—

Exercise 9. Design a function encrypt-string which takes a String and returns the rotated representation of the String. Use the built-in function explode, which takes a String and converts it to a ListOf1Strings.

Exercise 10. Design a function encrypt-string-to-file that takes two Strings (a message to encrypt and a file name) and writes the encrypted message to the given file name. Use the write-file function from the 2htdp/batch-io library. The write-file function returns the given file name, and your function should also return the given file name.

Finally, make sure you can read your own secret messages using decrypt-file.

#### 3PunyyratrðŸ”—

Exercise 11. Qrfvta gur shapgvbaf rapelcg-fgevat-a naq qrpelcg-fgevat-a juvpu gnxr n Fgevat naq n AnghenyAhzore a naq rapelcg (be qrpelcg) gur fgevat hfvat EBGa. Gung vf, (rapelcg-fgevat-a f 13) fubhyq or gur fnzr nf (rapelcg-fgevat f).

Exercise 12. Guvf rkrepvfr vf rapelcgrq jvgu EBG17.

Uvjzxe kyv wletkzfe kizkyvdzlj-tzgyvi. Zk jyflcu ljv ifkrkzfe czbv vetipgk-jkizex, slk zk jyflcu ifkrkv vrty tyrirtkvi rttfiuzex kf zkj gfjzkzfe ze kyv jkizex. Kyrk zj, zk jyflcu rtk czbv IFK1 fe kyv wzijk tyrirtkvi, czbv IFK13 fe kyv 13ky tyrirtkvi, IFK26 (nyzty ufvj efkyzex) fe kyv 26ky tyrirtkvi, reu IFK1 fe kyv 27ky tyrirtkvi rxrze. Kyve uvjzxe r uvtipgkzfe wletkzfe.