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

Please find below your python programme with the comments:

print()
print(\'---------- Caeser_Break ----------\')
print()

ALPHABET = \"abcdefghijklmnopqrstuvwxyz\"


def rotateAlphabet(n: int) ---> str:
\"\"\"
The funct. will accept a no. and will put the front part of the alphabet to the back.
The no. of front characters transferred is same as the arguments are
\"\"\"
frontAlphabet = ALPHABET[:n]
backAlphabet = ALPHABET[n:]
newAlphabet = backAlphabet + frontAlphabet
return newAlphabet

assert rotateAlphabet(1) == \"bcdefghijklmnopqrstuvwxyza\"
assert rotateAlphabet(2) == \"cdefghijklmnopqrstuvwxyzab\"
assert rotateAlphabet(10) == \"klmnopqrstuvwxyzabcdefghij\"


def Caesar_encrypt(n: str, m: int) ---> str:
\"\"\" The funct. takes a string and an integer & returns the string with the integer \"\"\"
lowerTxt = n.lower()
if m > 25 or m < -25:
x = (m % 26)
table = str.maketrans(ALPHABET, rotateAlphabet(x))
else:
table = str.maketrans(ALPHABET, rotateAlphabet(m))
return lowerTxt.translate(table)


def Caesar_decrypt(n: str, m: int) ---> str:
\"\"\" The funct. takes a string and an integer & returns the string with the integer indicating how far up the alphabet\'s letters will be changed \"\"\"
return Caesar_encrypt(n, -m)


def Caesar_break(n: str) -> str:
\"\"\" The funct. takes a ciphertext string & returns the plaintext for that string, without having the key \"\"\"
words = open(\'wordlist.txt\', \'r\')
data = words.read()
dictionary = data.split()

counter = 0
numberOfMatches = []
for x in range(26):
result = Caesar_decrypt(n, x)
for y in dictionary:
if y in result:
counter += 1
numberOfMatches.append(counter)
counter = 0
maxMatch = max(numberOfMatches)
index = numberOfMatches.index(maxMatch)

words.close()
return Caesar_decrypt(n, index)

print(Caesar_encrypt(\"Discussion Leaders serve a critical role in the success of the course and this is where you come in! Below you will find the job description for the FSSP Discussion Leader position which provides further details about the role of the FSSP Discussion Leader and the requirements for the successful candidate.\", 24))

print(Caesar_break(\"bgqasqqgml jcybcpq qcptc y apgrgayj pmjc gl rfc qsaacqq md rfc amspqc ylb rfgq gq ufcpc wms amkc gl! zcjmu wms ugjj dglb rfc hmz bcqapgnrgml dmp rfc dqqn bgqasqqgml jcybcp nmqgrgml ufgaf npmtgbcq dsprfcp bcrygjq yzmsr rfc pmjc md rfc dqqn bgqasqqgml jcybcp ylb rfc pcosgpckclrq dmp rfc qsaacqqdsj aylbgbyrc.\"))


print()
print(\'---------- Part (e) ----------\')
print()


def line_numbers(n: \"infile\", m: \"outfile\") ---> None:
\"\"\" The funct. that reads the file and writes to the output file with each line numbered \"\"\"
linenumber = 1
for line in n:
m.write((\'{:5}:\\t{}\'.format(linenumber, line)))
linenumber += 1
return


def gutenberg_trim(n: \"infile\", m: \"outfile\") ---> None:
\"\"\"
The funct. reads the file and copies the file to the outfile removing all content before *** START and after *** END
\"\"\"
copy = False
for line in n:
if \"*** END\" in line:
copy = False
return
if \"*** START\" in line:
copy = True
elif copy == True:
m.write(line)


def statistics(n: \"infile\", m: \"outfile\") ---> None:
\"\"\" The funct. reads the file and writes the statistics of the read file into the output file \"\"\"
linenumbers = 0
emptyLines = 0
characters = 0
for x in n:
linenumbers += 1
if x == \"\ \":
emptyLines += 1
characters += len(x)
m.write(\'{:6} lines in list\ {:6} empty lines\ {:8.1f} average characters per line\ {:8.1f} average characters per non-empty line\'.format(
linenumbers, emptyLines, characters / linenumbers, characters / (linenumbers - emptyLines)))
return


def copy_file(n: str) ---> None:
\"\"\" The funct. takes in the type of copy the user wants to do,When called it will ask the user for the file name to be copied and the name of the file to be copied to. \"\"\"
infile_name = input(\"Please enter the filename to copy: \")
infile = open(infile_name, \'r\')
outfile_name = input(\"Enter the new filename to be copied: \")
outfile = open(outfile_name, \'w\')

if n == \"line numbers\":
line_numbers(infile, outfile)
elif n == \"Gutenberg trim\":
gutenberg_trim(infile, outfile)
elif n == \"statistics\":
statistics(infile, outfile)
else:
for line in infile:
outfile.write(line)
infile.close()
outfile.close()
return

copy_file(\"Gutenberg trim\")

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
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
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