Answered step by step
Verified Expert Solution
Question
1 Approved Answer
simpleSubHacker.py: https://inventwithpython.com/simpleSubHacker.py Problem 3 In chapter 18, you saw a method for hacking the simple substitution cipher. However, this method cannot always decipher every letter,
simpleSubHacker.py: https://inventwithpython.com/simpleSubHacker.py
Problem 3 In chapter 18, you saw a method for hacking the simple substitution cipher. However, this method cannot always decipher every letter, sometimes resulting in blanks sprinkled throughout the recovered plaintexts. For example, hacking the message Sy 1 nlx sr pyyacao 1 ylwj eiswi upar lulsxrj isr sxrjsxwjr, ia esmm rwctjsxsza sj wmpramlh lxo txmarr jia aqsoaxwa sr pqaceiamnsxu, ia esmm caytra jp famsaqa sj. Sy, px jia pjiac ilxo, ia sr pyyacao rpnajisxu eiswi lyypcor 1 sxrisxwir, ia esmm lwwabi si agax px iia rmsuijarj aqsoaxwa. Jia pcsusx py nhjir sr agbmlsxao sx jisr elh. -Facjclxo Ctrramnm with the simpleSubHacker.py program given in Chapter 18 results in the following decryp tion: If a man is offered a fact which goes against his instincts, he vill scrutinize it closel.. and unless the evidence is overwhelming, he will refuse to _elieve it. If, on the other hand, he is offered something which affords a reason for acting in accordance to his instincts, he will acce_t it even on the slightest evidence. The origin of m_ths is e__lained in this wa -_ertrand Russell So, the mapping of the cipher letters is incomplete, leaving some blanks in our deciphered text. We can see that, for example, the cipher letter ''has not been mapped to the plaintext letter 'y', and so the cipher word 'wmpramh' is only partially deciphered as 'closel.'. However, looking at dictionary.txt, the only word which begins with 'closel' is, in fact, 'closely', so, making the reasonable assumption that the correct decipherment of wmpramh' is a word in dictionary.txt, the decipherment of 'wmpramh' must be 'closely', and thus cipher 'h' must map to plaintext 'y'. This deduction also reveals that 'nhjir' and 'ely', initially partially deciphered as 'm.ths' and 'wa.', should decipher to 'myths' and 'way' respectively. Going one step further, 'lwwabj' is initially incompletely deciphered to 'acce.t', which could be 'accent' or accept'. However, the initial decipherment pass mapped cipher 'x' to plaintext n'. Since a simple substitution cipher key must be one-to-one, the only option for acce t' is accept', so we can add to our mapping that cipher 'b' maps to plaintext 'p Finally, 'elained' can only be 'explained', and ertrand' can only be 'Bertrand' (again, assuming the plaintext does not contain any strange words), thus mapping cipher 'g' to plaintext X, and cipher 'f, to plaintext b. In short, by making a reasonable assumption, we were able to add to the mapping produced by simpleSubHacker.py, further mapping .h' to 'y', 'b, to p', g, to , 'f, to 'b. With this extended key, we can complete the decipherment: If a man is offered a fact which goes against his instincts, he will scrutinize it closely, and unless the evidence is overwhelming, he will refuse to believe it. If, on the other hand, he is offered something which affords a reason for acting in accordance to his instincts, he will accept it even on the slightest evidence. The origin of myths is explained in this way. -Bertrand Russell In this problem, your task is to automate this 'second pass' for solving simple substitution ciphers You have learned about regular expressions, and how they can be used to identify whether or not a given word matches a given pattern. Use this knowledge to create a python module 'modifiedSimpleSubHacker.py', containing a function 'hackSimpleSub', which first runs the de- cipherment algorithm used by 'simpleSubHacker.py', obtaining both the decipherment and the key it produces. Then, if there are any blanks in this initial decipherment, your code should use regular expressions to identify words in 'dictionary.txt' which match the patterns of the words containing those blanks (for example, the pattern induced by 'acce.t' is matched by 'accent' and accept'), and, if possible (i.e. if doing so does not violate the one-to-one constraint of the simple substitution key), adds any induced mappings to the key. Once this is done, the hackSimpleSub subroutine in modifiedSimpleSubHacker.py should produce a final decipherment, in which as many of the blanks as possible are filled in, and return the string containing this decipherment. For example, given the ciphertext from the above example, the 'hackSimpleSub' function in yourmodifiedSimpleSubHacker.py, should return the final, underscore-free plaintext above Problem 3 In chapter 18, you saw a method for hacking the simple substitution cipher. However, this method cannot always decipher every letter, sometimes resulting in blanks sprinkled throughout the recovered plaintexts. For example, hacking the message Sy 1 nlx sr pyyacao 1 ylwj eiswi upar lulsxrj isr sxrjsxwjr, ia esmm rwctjsxsza sj wmpramlh lxo txmarr jia aqsoaxwa sr pqaceiamnsxu, ia esmm caytra jp famsaqa sj. Sy, px jia pjiac ilxo, ia sr pyyacao rpnajisxu eiswi lyypcor 1 sxrisxwir, ia esmm lwwabi si agax px iia rmsuijarj aqsoaxwa. Jia pcsusx py nhjir sr agbmlsxao sx jisr elh. -Facjclxo Ctrramnm with the simpleSubHacker.py program given in Chapter 18 results in the following decryp tion: If a man is offered a fact which goes against his instincts, he vill scrutinize it closel.. and unless the evidence is overwhelming, he will refuse to _elieve it. If, on the other hand, he is offered something which affords a reason for acting in accordance to his instincts, he will acce_t it even on the slightest evidence. The origin of m_ths is e__lained in this wa -_ertrand Russell So, the mapping of the cipher letters is incomplete, leaving some blanks in our deciphered text. We can see that, for example, the cipher letter ''has not been mapped to the plaintext letter 'y', and so the cipher word 'wmpramh' is only partially deciphered as 'closel.'. However, looking at dictionary.txt, the only word which begins with 'closel' is, in fact, 'closely', so, making the reasonable assumption that the correct decipherment of wmpramh' is a word in dictionary.txt, the decipherment of 'wmpramh' must be 'closely', and thus cipher 'h' must map to plaintext 'y'. This deduction also reveals that 'nhjir' and 'ely', initially partially deciphered as 'm.ths' and 'wa.', should decipher to 'myths' and 'way' respectively. Going one step further, 'lwwabj' is initially incompletely deciphered to 'acce.t', which could be 'accent' or accept'. However, the initial decipherment pass mapped cipher 'x' to plaintext n'. Since a simple substitution cipher key must be one-to-one, the only option for acce t' is accept', so we can add to our mapping that cipher 'b' maps to plaintext 'p Finally, 'elained' can only be 'explained', and ertrand' can only be 'Bertrand' (again, assuming the plaintext does not contain any strange words), thus mapping cipher 'g' to plaintext X, and cipher 'f, to plaintext b. In short, by making a reasonable assumption, we were able to add to the mapping produced by simpleSubHacker.py, further mapping .h' to 'y', 'b, to p', g, to , 'f, to 'b. With this extended key, we can complete the decipherment: If a man is offered a fact which goes against his instincts, he will scrutinize it closely, and unless the evidence is overwhelming, he will refuse to believe it. If, on the other hand, he is offered something which affords a reason for acting in accordance to his instincts, he will accept it even on the slightest evidence. The origin of myths is explained in this way. -Bertrand Russell In this problem, your task is to automate this 'second pass' for solving simple substitution ciphers You have learned about regular expressions, and how they can be used to identify whether or not a given word matches a given pattern. Use this knowledge to create a python module 'modifiedSimpleSubHacker.py', containing a function 'hackSimpleSub', which first runs the de- cipherment algorithm used by 'simpleSubHacker.py', obtaining both the decipherment and the key it produces. Then, if there are any blanks in this initial decipherment, your code should use regular expressions to identify words in 'dictionary.txt' which match the patterns of the words containing those blanks (for example, the pattern induced by 'acce.t' is matched by 'accent' and accept'), and, if possible (i.e. if doing so does not violate the one-to-one constraint of the simple substitution key), adds any induced mappings to the key. Once this is done, the hackSimpleSub subroutine in modifiedSimpleSubHacker.py should produce a final decipherment, in which as many of the blanks as possible are filled in, and return the string containing this decipherment. For example, given the ciphertext from the above example, the 'hackSimpleSub' function in yourmodifiedSimpleSubHacker.py, should return the final, underscore-free plaintext aboveStep 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