Question
You want to send a secret message to your friend. You decide to agree on a simple substitution strategy. You will scramble up the letters
You want to send a secret message to your friend. You decide to agree on a simple substitution strategy. You will scramble up the letters of the alphabet to create a codebook. Well use the scrambled alphabet to create the encoded text (the ciphertext) from our message by simple replacement. For example, the scrambled alphabet (codebook) could be: Plaintext Alphabet: abcdefghijklmnopqrstuvwxyz Ciphertext Alphabet: xpyrbcazmqkinsowelfutdhjvg So, every a in our message is replaced by an x; every b is replaced with a p and so on. For example Plaintext message: attack at dawn Ciphertext: xuuxyk xu rxws (notice that k is encoded with k -- this is fine; we still have a scrambled alphabet). The scrambled alphabet gives us a function from letters to letters; the inverse of thisd function tells us how to decode. Try decoding this ciphertext using the encoding above. Ciphertext: wloalxn rbfmas uho mf xhbfonb Assignment Overview: You will write two C programs: encode and decode. The encode program will read clear text from stdin and write the corresponding ciphertext to stdout. Similarly, decode will read ciphertext from stdin and write the original clear text to stdout. But wait! We probably want different encodings for communicating with different people. For example, imagine three people work for a spy agency: ALICE is the is the boss at headquarters BOB is a field agent who works for ALICE CATHY is also a field agent working for ALICE Alice needs to send secret/encoded messages to Bob and Cathy. So they will both need decoders. But Alice wants the messages sent to Bob to be secret from Cathy and the messages she sends to Cathy to be secret from Bob. Alice to Cathy: I think Bob is a double-agent Alice to Bob: I think Cathy is on to us! (Are Alice and Bob up to something?) If Bob starts reading Cathys email, he should not be able to decode her messages from Alice in the same way he decodes messages sent to him. So use different encodings for different recipients! Lets think about how you might create an scrambled alphabet for encoding and decoding messages -- i.e., generate a random codebook? Heres an idea (in C). Use the the C rand() function to shuffle the letters a..z. Then we could share the resulting codebook with the appropriate people. But rand() is actually pseudo-random -- it will produce the exact same code book every time we run it unless we use a different seed. On the other hand, different seeds will produce distinct pseudo-random sequences; so we can just use different seeds to generate different codebooks. This also means that we can use the seeds as Keys (like a PIN). In our example: KEY1: known by Alice and Bob, but not Cathy. KEY2: known by Alice and Cathy, but not Bob. This way, we can use integer keys (like PINs) rather than memorizing the codebooks themselves. Once a key is used to seed the random number sequence, we the corresponding codebook can be generated. The Assignment: Getting the Key The encode and decode programs will take a key value and use it to generate (re-generate) the corresponding codebook. This key is just an integer and is used to seed the random number generator (via srand()) The key will be specified through a command-line argument. For example, to encode using the key 31415 I would type this at the shell: % encode 31415 To decode a message that used this key: % decode 31415 Reading command line arguments [Reading: Chapter 17, Page 380 of Kochan] The main function in a C program can actually take parameters -- the argc and argv parameters (for argument count and argument vector respectively). The argument vector is an array of strings from the command entered to run the program. Try this: #include int main(int argc, char *argv[]) { int i; for(i=0; i out.txt Whatever the program prints to stdout (probably through printf) will be redirected to the file out.txt. In the case of this project, you could do this: % encode 1324 < msg.txt > cipher.txt % decode 1324 < cipher.txt > decode.txt If everything is working ok, the contents of decode.txt and msg.txt should be the same after this.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started