Now were going to do some cryptanalysis of our own Write a f

Now we\'re going to do some cryptanalysis of our own. Write a function called Caesar_break that takes a ciphertext string (encrypted using a Caesar cipher as we did last week) and returns the plaintext for that string, without having the key.

We\'ll take a \"brute force\" approach:

There are 26 possible keys; we\'ll generate a decryption alphabet for each of these 26 keys.

We\'ll \"decrypt\" the ciphertext using each of the 26 alphabets. (Only one of these attempted decryptions will be the correct plaintext message, of course. But we don\'t know which one in advance. Trying all the possibilities is what we mean when we call this a \"brute force\" approach.)

For each of the 26 possibly-decrypted messages, our program needs to figure out whether it \"looks like English\" instead of encrypted gibberish. Here\'s how: We\'ll take each word in the possibly-decrypted message and look it up in a dictionary (a list of English words). If the word is in the dictionary, then it\'s an English word; if there are a lot of English words in this possibly-decrypted message, it\'s likely that this message is the correct decrypted plaintext. (If very few words in the message are in the dictionary, then this message isn\'t the English plaintext.) So we need to count up how many of the words in each possibly-decrypted message we find in the dictionary, saving that total along with the message that produced it.

Once we\'re done with all 26 possible decryptions, we should expect that the possibly-decrypted message that had the most \"hits\" in the dictionary is in fact the correctly decrypted plaintext, and that\'s the message we return.

To get the dictionary, download the file http://www.ics.uci.edu/~kay/wordlist.txt onto your machine and read it in to your program. Remove newline characters if necessary.

[Here are some other suggestions and hints. Don\'t automatically read them; use them just as needed. Trying to think out the solution yourself is what builds the new neural pathways in your brain (i.e., that\'s how you learn). (i) Use your rotated-alphabet function from last week to help in making your list of 26 encryption/decryption tables. (ii) Choose a few sentences, encrypt them with your Caesar_encrypt function from last week, and use them to test your Caesar_break function. (iii) At some point you\'ll want to break your possibly-decrypted string into words, removing white space and punctuation, so you can look each word up in the dictionary. But also hang on to the original possibly-decrypted string with the spaces and punctuation, because that\'s the version you\'ll want to return if it\'s identified as the plaintext.]

***ALL IN PYTHON LANGUAGE PLEASE***

Solution

# lab 7 print(\'---------------part d-----------------\') print() print() ALPHABET = \'abcdefghijklmnopqrstuvwxyz\' def rotate_alphabet(numb2: int)->str: alph_obj = ((ALPHABET + ALPHABET[:numb2%26])) return alph_obj def Caesar_encrypt(s: str, numb: int): alph_obj = rotate_alphabet(numb) table = str.maketrans(\'abcdefghijklmnopqrstuvwxyz\', alph_obj[numb%26:]) return s.translate(table) print(Caesar_encrypt(\'hello my name is vladimir putin\', 31)) def Caesar_decrypt(d: str, numb2: int): alph_obj = rotate_alphabet(numb2) table = str.maketrans(alph_obj[numb2%26:], \'abcdefghijklmnopqrstuvwxyz\') return d.translate(table) print(Caesar_decrypt(Caesar_encrypt(\'hello my name is Vladimir Putin\', 5), 5)) def strip_punctuation(word:str)->str: result = \"\" punctuation = \",.?!:;-()_[]/{}<> \" for obj in word: if obj not in punctuation: result += obj return result infile = open(\'wordlist.txt\') dictionary = infile.read() infile.close() def Caesar_break(cipher:str) -> str: alph_obj = cipher.split() for obj in range(len(temp)): alph_obj[obj] = strip_punctuation(alph_obj[i]) top_count = 0 match = 0 for x in range(26): count = 0 for a in temp: if Caesar_decrypt(a, x) in dictionary: count += 1 if count >= top_count: top_count = count match = x return Caesar_decrypt(cipher, match) print(\'---------------part d.2-----------------\') print() print() def Caesar_break(cipher:str) -> str: temp = cipher.split() for i in range(len(temp)): temp[i] = strip_punctuation(temp[i]) top_count = 0 match = 0 for i in range(26): count = 0 for n in temp: if Caesar_decrypt(n, i) in dictionary: count+=1 if count >= top_count: top_count = count match = i return Caesar_decrypt(cipher, match) print(Caesar_break(\'mjqqt rd sfrj nx aqfinrw\'))
Now we\'re going to do some cryptanalysis of our own. Write a function called Caesar_break that takes a ciphertext string (encrypted using a Caesar cipher as we

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site