Question: implement the chi_squared_test(), guess_letter(), and freq_analysis() functions. Pay notice to the fact that you know the key length, greatly simplifying what you need to implement.

implement the chi_squared_test(), guess_letter(), and freq_analysis() functions. Pay notice to the fact that you know the key length, greatly simplifying what you need to implement.

The freq_analysis() function above should make calls to guess_letter() which in turn should make calls to chi_squared_test().

HINT 1: You will more than likely need a working Vigenre Cipher implementation for this challenge, at the very least, to test your solution.

HINT 2: You may find the ACTUAL_FREQUENCY global variable useful for your implementation of chi_squared_test()!

#!/usr/bin/env python3 import string import vigenere

# String of all 26 alphabetical letters in uppercase ALPHABET = string.ascii_uppercase # Letter frequencies: https://en.wikipedia.org/wiki/Letter_frequency ACTUAL_FREQUENCY = [ 0.082, # A 0.015, 0.028, 0.043, 0.13, 0.022, 0.02, 0.062, 0.07, 0.0015, 0.0077, 0.04, 0.024, 0.067, 0.075, 0.019, 0.00095, 0.06, 0.063, 0.091, 0.028, 0.0098, 0.024, 0.0015, 0.02, 0.00074, # Z ]

def freq_analysis(ciphertext: str, key_size: int) -> str: """ TODO: Replace this with an explanation of your implementation """ # TODO: Replace this with your implementation

def guess_letter(ciphertext: str) -> str: """ TODO: Replace this with an explanation of your implementation """ # TODO: Replace this with your implementation

def chi_squared_test(plaintext: str) -> float: """ TODO: Replace this with an explanation of your implementation """ # TODO: Replace this with your implementation

if __name__ == "__main__": from test import KEYS, TESTS

for plaintext in TESTS: for key in KEYS: ciphertext = vigenere.encrypt(plaintext, key) assert key == freq_analysis(ciphertext, len(key))

The test.py file below simply contains some keys and plaintext you can use to test your solutions, e.g., by encrypting plaintexts with given keys and testing that your freq_analysis() function correctly predicts the key, given the ciphertext. We will be testing your code with different keys/plaintext.

KEYS = ["N", "BOG", "HOBBIT", "ELF"] TESTS = [ "THISPAGESAFOUNDHASTHATREDBECOMEHIMJOURNEYLATERAGEKNOWSOMEACOLLECTEDHOBBITSFORMERLYQUIETWASUNDERSTANDWATERMILLEVENOFTHEYFINDTHEYUNNECESSARILYTHEYSILENTLYMEETUNTILNEVERELUSIVENESSANDRENDEREDAISHEIGHTOURHIVETALLEROFTO", "BOOKALITTLEINALREADYSTORYBOOKFAMOUSTHEREINTOINVOLVEDTHATMOREMAYFEWFROMARETHANANDTHEIRORORINTHEAVOIDTHEYARETHEYPOSSESSEDWHENCOMETOINISPRACTICEINIMITABLELITTLEEVENISMEASUREDWINDLEDACCORDINGISENGRIMRIDE", "ISREADEROFTHEBEENWASCOMPOSEDINANDTHEALLAREABOUTNOTNOTESHOBBITLOREANTHEYGOODFAVOURITELIKEAANCIENTBIGUSAREINCLINEDAREFROMLARGEBLUNDERINGMENFACTDUEANDBYPEOPLEWHENVARIABLETHEYTHEYTOTHEA", "LARGELYMAYTHEIRSELECTIONPUBLISHEDDERIVEDBYTHEBACKEASTTHEHERETHISPOSSESSONANDUNOBTRUSIVEARETILLEDHAUNTMACHINESHANDLOOMDAYSFOLKWITHQUICKTONONETHELESSTHEFOLKBYITSTUDIEDSOLELYABIGGERSMALLERTHEYRANGINGSELDOMSAYTHESECOND", "CONCERNEDDISCOVERHISTORYFROMUNDERFROMBILBOWORLDAGAINANDHOBBITSRELATEDREMARKABLETHETHETHEBUTTODAYEARTHTHEYMORETHOUGHTHEYASDISMAYOFBENIMBLEFIRSTWHOMANDMAYMAGICTOCLOSEANDTHANAREBETWEENNOWANDREDWAS", "WITHMUCHFURTHERTHETHETHEHIMSELFATSINCEHISINMANYPEOPLEEARLIERMOREFIRSTVERYFORADOCOMPLICATEDTHEYWERETHEYANDHEARINGFATANDTHETHEYTHISSEEMOFAFRIENDSHIPCLUMSIERDWARVESNOTTWOREACHINBOOKFOUR", "HOBBITSOFINFORMATIONREDTITLEEARLIERTHELARGETHEYRETURNTHEHOWEVERFROMBOOKIMPORTANTADVENTUREANCIENTTHEYWELLORDEREDNOTTHANWEREASCALLAREANDANDDEFTARTDOANMAGICALANYPROFESSIONALWITHRACESLESSACTUALLYANDTHREEANCIENTBANDOBRASFOOT", "ANDTHEIRWILLBOOKOFCHAPTERSFIRSTANDTOLDANGREATEVENTSMAYTHEFORPOINTSISPEOPLELOVEANDANDASKILFULAUSBECOMINGSHARPEYEDDOINOFNOTTHEYBUTKINDSKILLTHEFORTOUTMUCHFOURFEETDAYSTOOKFIVE", "FROMCHARACTERALSOOFTHEOFHOBBITCALLEDOFADVENTUREOFWISHOUTSETSUCHAREBRIEFLYMOREPEACEWELLFARMEDDIDFORGEBELLOWSWITHRULEANDHARDANDNOTTHEIRDISAPPEARINGWISHHAVEHOBBITSANDTHATEARTHTHEYANDSHORTERFEETBUTTHEYBULLROARERAND" "ITSANDBEWESTMARCHTHATHOBBITTHETOBYHISWHICHTHATTOWHILEREADERSHERERECALLEDNUMEROUSANDCOUNTRYSIDENOTATOOLSSHYNOWTOTHOUGHHURRYMOVEMENTSSWIFTLYANDTODEVELOPEDHAVETHEIRHEREDITYHAVEARESTOCKYTHATTHEIROFTHEYWERESONABLE", ]

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!