Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Assignment Overview The Caesar cipher is named after Julius Caesar who used this type of encryption to keep his military communications secret. A Caesar cipher
Assignment Overview The Caesar cipher is named after Julius Caesar who used this type of encryption to keep his military communications secret. A Caesar cipher replaces each plain-text letter with one that is a fixed number of places down (or up) the alphabet. The plain-text is your original message; the cipher-text is the encrypted message. The example shown below is a shift of three so that B in the plain-text becomes E in the cipher-text, a C becomes F, and so on. The mapping wraps around so that X maps to "A" and so on. XIY ZAB|C|D|E|F ABCDEFGHI Here is the complete mapping for a shift of three: Plain: ABCDEFGHIJKLMNOPQRSTUVWXYZ Cipher: DEFGHIJKLMNOPQRSTUVWXYZABC To encrypt a message simply substitute the plain-text letters with the corresponding cipher-text letter. For example, here is an encryption of the quick brown fox jumps over the lazy dog using our shift- three cipher (case is ignored): Plaintext: the quick brown fox jumps over the lazy dog Ciphertext: WKH TXLFN EURZQ IRA MXPSV RYHU WKH ODCB GRJ To decrypt the message simply reverse the process. The encryption can also be represented using modular arithmetic after first transforming the letters into numbers according to the scheme: A=0, B = 1, etc. (which is the index of the alphabet if it is in a string or a list). A shift-three cipher will take the number of each letter (plaintextChar), add 3 (the shift), and then find the remainder after dividing by 26 to get the cipherText: cipherTextChar = (plaintextChar + 3) % 26 Program Specifications Your task is to write a program that can decrypt a message that has been encoded using a Caesar cipher. Stated another way, you need to find the shift for the cipher. Once you determine the shift, you know the mapping so you can decrypt the message. Caesar Cipher Cracking To find the shift you need to know about cracking Caesar ciphers using a technique that has been around for a thousand years. Any language such as English has a known distribution for each letter. For example, the letter "E" is the most common letter in English making up 12.702% of the letters on average (ignoring case). The letter T" is next (9.056%), followed by A (8.17%), and so on (for all see Wikipedia). The order E-T-A-... is what matters for decryption, not the percentage. Note that this ordering might not apply if the text is short. The procedure begins by finding the most common letter. You can guess and code) that the most common letter maps to "E. You can now find the "shift" from the most common letter in the cipher- text to the expected most common letter E. For example, if the most common letter in the cipher- text is "H", you know that the shift from "E" to "H" is 3. What about spaces between words and punctuation? In real world cipher-text there are no spaces or punctuation because those are useful clues for deciphering. Similarly, case can provide clues so case doesn't matter. In the cipher-text we provide for this project we have left in both the punctuation and spaces because they will be helpful for you to recognize that your deciphering is correct or not. You will need to ignore spaces and punctuation when counting letters in the cipher-text (if you forget to ignore them, beware that the space will be the most common character). Your high level algorithm will be: 1. Input the cipher-text. 2. Find the most common character. 3. Find the shift from E to that most common character. 4. Use the shift to decode each character of the cipher-text and print the resulting plain-text. (Hint: stop here for a simple version to start with.) 5. If the plain-text isn't readable English, try the next-most common character and continue checking successive next-most-common characters until the resulting plain-text is readable. Your program must also meet the following specifications: 1. You must have and use at least these four functionsmore are fine. A proj04.py file with function stubs is provided. a. def decode_char (ch, shift) char i. The parameters are ch, a string character, and shift, an int. This function returns the shifted decoding of character ch, i.e. decode ch. If the shift is -3 and the cipher-text character is H, this function will return the plain-text character E. If the parameter ch is not a letter, simply return it (do not try to decode it) ii. Parameters: ch, int iii. Returns: ch iv. Display: nothing b. def get_shift(s, ignore) int, char i. The parameters s and ignore are both strings. Return the shift (i.e., the decode key) for strings, and the most common character (if more than one are equally common, return the first letter that occurs in the alphabet). If the text is long enough, we expect the most common character to be "E" so shift would be the shift to get "E" (hint: what is the difference between the position of the most common character and the position of E in the alphabet). For example, if the most common character in the cipher-text is H, the shift will be -3, the number of characters to get to E. In that case you would return -3 and H. The ignore parameter is the string of cipher-text letters to ignorethey have been tried but didn't result in readable English text. The ignore parameter allows you to find the next-most-common character by ignoring more common characters. Hint: You may set ignore = "" when testing the function with the function test case below. ii. Parameters: str, str iii. Returns: int, char iv. Display: nothing c. def output_plaintext(s, shift) i. The parameters are s, a string, which is the cipher-text and shift, an int. Output plain-text of the cipher-text using the key, i.e. using the shift. Print the output in upper case. You must use your decode_char function to decode each character of s. Begin your output with a blank line. ii. Parameters: str, int iii. Returns: nothing iv. Display: str d. def main() The main program that calls the other functions don't forget the call to main, i.e. main() - it is already called for you at the bottom of the skeleton code. Having main allows us to more easily create a set of test programs to test the other functions because we need to comment out main for those tests (this is what Mimir does when doing a function specific test case). The main function must do the following: i. Prompt for an input cipher-text string. ii. Get a key (a.k.a. shift) for the cipher-text string iii. Display the plaintext. iv. Ask the user if the plaintext is readable English (yeso) 1. If the response is not no, print a message (check the string.txt file) and end the program. 2. If the response is no, add the key letter to the ignore string and go back to step ii to get a new key for the string Deliverables The deliverable for this assignment is the following file: proj04.py -- your source code solution Be sure to use the specified file name and to submit it for grading via Mimir before the project deadline. Notes and Hints: 1. To clarify the project specifications, sample output is appended to the end of this document. 2. Items 1-9 of the Coding Standard will be enforced for this projectnote the change to include more items. 3. In main, your prompt for yeso must accept any case for the characters, e.g. Es is acceptable as "yes". 4. You can test functions separatelythat can be a huge advantage in developing correct code faster! If you cannot figure out how to do that, ask your TA for guidance. 5. One way to decrypt is to use the two functions that are inverses of each other: ord() and chr (). These work because our letters are based on the ASCII subset of Unicode. However, you are forbidden to use ord() and chr() in this assignmentit is poor coding to be dependent on ASCII. Using those functions will result in a zero for the assignment. 6. You do not need to check for any input errors. 7. Do not hard code your solutions the result is a zero. Hard coding means that your program for the specific tests rather than having a generic solution. 8. You may not use advanced data structures such as lists, dictionaries, sets or classes in solving this problem. 9. As this is your first program with functions, do not forget function headers! Sample Interaction: Function Test decode_ch() Instructor: cipher-ch, shift, plain-ch: H 3 K Student: cipher-ch, shift, plain-ch: H 3 K Instructor: cipher-ch, shift, plain-ch: H-3 E Student: cipher-ch, shift, plain-ch: H-3 E Instructor: cipher-ch, shift, plain-ch: A -1 z Student: cipher-ch, shift, plain-ch: A -1 z Instructor: cipher-ch, shift, plain-ch: Z 1 A Student: cipher-ch, shift, plain-ch: Z 1 A Instructor: cipher-ch, shift, plain-ch: ZOZ Student: cipher-ch, shift, plain-ch: ZO Z Function Test get_shift () Cipher text: JI QNPJX YT JFY WJI GJJYX. Instructor: shift, max_ch: -5 J Student: shift, max_ch: -5 J ignore: J Instructor: shift,max_ch: -20 Y Student: shift,max_ch: -20 Y (Of course, our test cases come from Shakespeare's Julius Caesar!) Test Case 1 Cracking a Caesar cypher. Input cipherText: Ji qnpjx yt jfy wji gjjyx. ED LIKES TO EAT RED BEETS. Is the plaintext readable as English? (yeso): yes Done. Test Case 2 Cracking a Caesar cypher. Input cipherText: Frzdugv glh pdqb wlphv ehiruh wkhlu ghdwkv; Wkh ydoldqu qhyhu wdvwh ri ghdwk exw rqfh. Ri doo wkh zrqghuv wkdw L bhw kdyh khdug, Lw vhhpv wr ph prvw vwudqjh wkdw phq vkrxog indu; Vhhlqj wkdw ghdwk, d ghfhvvdub hag, zloo frph zkhq lw zloo frph. COWARDS DIE MANY TIMES BEFORE THEIR DEATHS; THE VALIANT NEVER TASTE OF DEATH BUT ONCE. OF ALL THE WONDERS THAT I YET HAVE HEARD, IT SEEMS TO ME MOST STRANGE THAT MEN SHOULD FEAR; SEEING THAT DEATH, A NECESSARY END, WILL COME WHEN IT WILL COME. Is the plaintext readable as English? (yeso): yes Test Case 3 Cracking a Caesar cypher. Input cipherText: Wlv d frpprq surri, Wkdw orzolghvv lv brxqj dpelwlrq'v odgghu JYI Q SECCED FHEEV, JXQJ BEMBYDUII YI OEKDW QCRYJYED'I BOTTUH Is the plaintext readable as English? (yeso): no PEO W YKIIKJ LNKKB, PDWP HKSHEJAOO EO UKQJC WIXEPEKJ'O HWZZAN Is the plaintext readable as English? (yeso): no FUE M OAYYAZ BDAAR, FTME XAIXUZQEE UE KAGZS MYNUFUAZ'E XMPPQD Is the plaintext readable as English? (yeso): no XMW E GSQQSR TVSSJ, XLEX PSAPMRIWW MW CSYRK EQFMXMSR'W PEHHIV Is the plaintext readable as English? (yeso): no KZJ R TEDDFE GIFFW, KYRK CFNCZEVJJ ZJ PFLEX RDSZKZFE'J CRUUVI Is the plaintext readable as English? (yeso): no ETD L NZXXZY ACZZQ, ESLE WZHWTYPDD TD JZFYR LXMTETZY'D WLOOPC Is the plaintext readable as English? (yeso): no MBL T VHFFHG IKHHY, MATM EHPEBGXLL BL RHNGZ TFUBMBHG'L ETWWXK Is the plaintext readable as English? (yeso): no LAK S UGEEGF HJGGX, LZSL DGODAFWKK AK QGMFY SETALAGF'K DSVVWJ Is the plaintext readable as English? (yeso): no UJT B DPNNPO OSPPG, UIBU MPXMJOFTT JT ZPVOH BNCJUJPO'T MBEEFS Is the plaintext readable as English? (yeso): no TIS A COMMON PROOF, THAT LOWLINESS IS YOUNG AMBITION'S LADDER Is the plaintext readable as English? (yeso): yes Scoring Rubric Computer Project #04 Scoring Summary General Requirements 5 pts Coding Standard 1-9 (descriptive comments, function header, etc...) Implementation: (6 pts) Function Test decode_ch Function Test get_shift 0__ (9 pts) 0 (6 pts) Test 1 0 (7 pts) Test 2 (7 pts) Test 3 Assignment Overview The Caesar cipher is named after Julius Caesar who used this type of encryption to keep his military communications secret. A Caesar cipher replaces each plain-text letter with one that is a fixed number of places down (or up) the alphabet. The plain-text is your original message; the cipher-text is the encrypted message. The example shown below is a shift of three so that B in the plain-text becomes E in the cipher-text, a C becomes F, and so on. The mapping wraps around so that X maps to "A" and so on. XIY ZAB|C|D|E|F ABCDEFGHI Here is the complete mapping for a shift of three: Plain: ABCDEFGHIJKLMNOPQRSTUVWXYZ Cipher: DEFGHIJKLMNOPQRSTUVWXYZABC To encrypt a message simply substitute the plain-text letters with the corresponding cipher-text letter. For example, here is an encryption of the quick brown fox jumps over the lazy dog using our shift- three cipher (case is ignored): Plaintext: the quick brown fox jumps over the lazy dog Ciphertext: WKH TXLFN EURZQ IRA MXPSV RYHU WKH ODCB GRJ To decrypt the message simply reverse the process. The encryption can also be represented using modular arithmetic after first transforming the letters into numbers according to the scheme: A=0, B = 1, etc. (which is the index of the alphabet if it is in a string or a list). A shift-three cipher will take the number of each letter (plaintextChar), add 3 (the shift), and then find the remainder after dividing by 26 to get the cipherText: cipherTextChar = (plaintextChar + 3) % 26 Program Specifications Your task is to write a program that can decrypt a message that has been encoded using a Caesar cipher. Stated another way, you need to find the shift for the cipher. Once you determine the shift, you know the mapping so you can decrypt the message. Caesar Cipher Cracking To find the shift you need to know about cracking Caesar ciphers using a technique that has been around for a thousand years. Any language such as English has a known distribution for each letter. For example, the letter "E" is the most common letter in English making up 12.702% of the letters on average (ignoring case). The letter T" is next (9.056%), followed by A (8.17%), and so on (for all see Wikipedia). The order E-T-A-... is what matters for decryption, not the percentage. Note that this ordering might not apply if the text is short. The procedure begins by finding the most common letter. You can guess and code) that the most common letter maps to "E. You can now find the "shift" from the most common letter in the cipher- text to the expected most common letter E. For example, if the most common letter in the cipher- text is "H", you know that the shift from "E" to "H" is 3. What about spaces between words and punctuation? In real world cipher-text there are no spaces or punctuation because those are useful clues for deciphering. Similarly, case can provide clues so case doesn't matter. In the cipher-text we provide for this project we have left in both the punctuation and spaces because they will be helpful for you to recognize that your deciphering is correct or not. You will need to ignore spaces and punctuation when counting letters in the cipher-text (if you forget to ignore them, beware that the space will be the most common character). Your high level algorithm will be: 1. Input the cipher-text. 2. Find the most common character. 3. Find the shift from E to that most common character. 4. Use the shift to decode each character of the cipher-text and print the resulting plain-text. (Hint: stop here for a simple version to start with.) 5. If the plain-text isn't readable English, try the next-most common character and continue checking successive next-most-common characters until the resulting plain-text is readable. Your program must also meet the following specifications: 1. You must have and use at least these four functionsmore are fine. A proj04.py file with function stubs is provided. a. def decode_char (ch, shift) char i. The parameters are ch, a string character, and shift, an int. This function returns the shifted decoding of character ch, i.e. decode ch. If the shift is -3 and the cipher-text character is H, this function will return the plain-text character E. If the parameter ch is not a letter, simply return it (do not try to decode it) ii. Parameters: ch, int iii. Returns: ch iv. Display: nothing b. def get_shift(s, ignore) int, char i. The parameters s and ignore are both strings. Return the shift (i.e., the decode key) for strings, and the most common character (if more than one are equally common, return the first letter that occurs in the alphabet). If the text is long enough, we expect the most common character to be "E" so shift would be the shift to get "E" (hint: what is the difference between the position of the most common character and the position of E in the alphabet). For example, if the most common character in the cipher-text is H, the shift will be -3, the number of characters to get to E. In that case you would return -3 and H. The ignore parameter is the string of cipher-text letters to ignorethey have been tried but didn't result in readable English text. The ignore parameter allows you to find the next-most-common character by ignoring more common characters. Hint: You may set ignore = "" when testing the function with the function test case below. ii. Parameters: str, str iii. Returns: int, char iv. Display: nothing c. def output_plaintext(s, shift) i. The parameters are s, a string, which is the cipher-text and shift, an int. Output plain-text of the cipher-text using the key, i.e. using the shift. Print the output in upper case. You must use your decode_char function to decode each character of s. Begin your output with a blank line. ii. Parameters: str, int iii. Returns: nothing iv. Display: str d. def main() The main program that calls the other functions don't forget the call to main, i.e. main() - it is already called for you at the bottom of the skeleton code. Having main allows us to more easily create a set of test programs to test the other functions because we need to comment out main for those tests (this is what Mimir does when doing a function specific test case). The main function must do the following: i. Prompt for an input cipher-text string. ii. Get a key (a.k.a. shift) for the cipher-text string iii. Display the plaintext. iv. Ask the user if the plaintext is readable English (yeso) 1. If the response is not no, print a message (check the string.txt file) and end the program. 2. If the response is no, add the key letter to the ignore string and go back to step ii to get a new key for the string Deliverables The deliverable for this assignment is the following file: proj04.py -- your source code solution Be sure to use the specified file name and to submit it for grading via Mimir before the project deadline. Notes and Hints: 1. To clarify the project specifications, sample output is appended to the end of this document. 2. Items 1-9 of the Coding Standard will be enforced for this projectnote the change to include more items. 3. In main, your prompt for yeso must accept any case for the characters, e.g. Es is acceptable as "yes". 4. You can test functions separatelythat can be a huge advantage in developing correct code faster! If you cannot figure out how to do that, ask your TA for guidance. 5. One way to decrypt is to use the two functions that are inverses of each other: ord() and chr (). These work because our letters are based on the ASCII subset of Unicode. However, you are forbidden to use ord() and chr() in this assignmentit is poor coding to be dependent on ASCII. Using those functions will result in a zero for the assignment. 6. You do not need to check for any input errors. 7. Do not hard code your solutions the result is a zero. Hard coding means that your program for the specific tests rather than having a generic solution. 8. You may not use advanced data structures such as lists, dictionaries, sets or classes in solving this problem. 9. As this is your first program with functions, do not forget function headers! Sample Interaction: Function Test decode_ch() Instructor: cipher-ch, shift, plain-ch: H 3 K Student: cipher-ch, shift, plain-ch: H 3 K Instructor: cipher-ch, shift, plain-ch: H-3 E Student: cipher-ch, shift, plain-ch: H-3 E Instructor: cipher-ch, shift, plain-ch: A -1 z Student: cipher-ch, shift, plain-ch: A -1 z Instructor: cipher-ch, shift, plain-ch: Z 1 A Student: cipher-ch, shift, plain-ch: Z 1 A Instructor: cipher-ch, shift, plain-ch: ZOZ Student: cipher-ch, shift, plain-ch: ZO Z Function Test get_shift () Cipher text: JI QNPJX YT JFY WJI GJJYX. Instructor: shift, max_ch: -5 J Student: shift, max_ch: -5 J ignore: J Instructor: shift,max_ch: -20 Y Student: shift,max_ch: -20 Y (Of course, our test cases come from Shakespeare's Julius Caesar!) Test Case 1 Cracking a Caesar cypher. Input cipherText: Ji qnpjx yt jfy wji gjjyx. ED LIKES TO EAT RED BEETS. Is the plaintext readable as English? (yeso): yes Done. Test Case 2 Cracking a Caesar cypher. Input cipherText: Frzdugv glh pdqb wlphv ehiruh wkhlu ghdwkv; Wkh ydoldqu qhyhu wdvwh ri ghdwk exw rqfh. Ri doo wkh zrqghuv wkdw L bhw kdyh khdug, Lw vhhpv wr ph prvw vwudqjh wkdw phq vkrxog indu; Vhhlqj wkdw ghdwk, d ghfhvvdub hag, zloo frph zkhq lw zloo frph. COWARDS DIE MANY TIMES BEFORE THEIR DEATHS; THE VALIANT NEVER TASTE OF DEATH BUT ONCE. OF ALL THE WONDERS THAT I YET HAVE HEARD, IT SEEMS TO ME MOST STRANGE THAT MEN SHOULD FEAR; SEEING THAT DEATH, A NECESSARY END, WILL COME WHEN IT WILL COME. Is the plaintext readable as English? (yeso): yes Test Case 3 Cracking a Caesar cypher. Input cipherText: Wlv d frpprq surri, Wkdw orzolghvv lv brxqj dpelwlrq'v odgghu JYI Q SECCED FHEEV, JXQJ BEMBYDUII YI OEKDW QCRYJYED'I BOTTUH Is the plaintext readable as English? (yeso): no PEO W YKIIKJ LNKKB, PDWP HKSHEJAOO EO UKQJC WIXEPEKJ'O HWZZAN Is the plaintext readable as English? (yeso): no FUE M OAYYAZ BDAAR, FTME XAIXUZQEE UE KAGZS MYNUFUAZ'E XMPPQD Is the plaintext readable as English? (yeso): no XMW E GSQQSR TVSSJ, XLEX PSAPMRIWW MW CSYRK EQFMXMSR'W PEHHIV Is the plaintext readable as English? (yeso): no KZJ R TEDDFE GIFFW, KYRK CFNCZEVJJ ZJ PFLEX RDSZKZFE'J CRUUVI Is the plaintext readable as English? (yeso): no ETD L NZXXZY ACZZQ, ESLE WZHWTYPDD TD JZFYR LXMTETZY'D WLOOPC Is the plaintext readable as English? (yeso): no MBL T VHFFHG IKHHY, MATM EHPEBGXLL BL RHNGZ TFUBMBHG'L ETWWXK Is the plaintext readable as English? (yeso): no LAK S UGEEGF HJGGX, LZSL DGODAFWKK AK QGMFY SETALAGF'K DSVVWJ Is the plaintext readable as English? (yeso): no UJT B DPNNPO OSPPG, UIBU MPXMJOFTT JT ZPVOH BNCJUJPO'T MBEEFS Is the plaintext readable as English? (yeso): no TIS A COMMON PROOF, THAT LOWLINESS IS YOUNG AMBITION'S LADDER Is the plaintext readable as English? (yeso): yes Scoring Rubric Computer Project #04 Scoring Summary General Requirements 5 pts Coding Standard 1-9 (descriptive comments, function header, etc...) Implementation: (6 pts) Function Test decode_ch Function Test get_shift 0__ (9 pts) 0 (6 pts) Test 1 0 (7 pts) Test 2 (7 pts) Test 3
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