Answered step by step
Verified Expert Solution
Question
1 Approved Answer
def kasiski _ diffs ( Y , case = upper ) : Y = only _ letters ( Y , case = case ) ctr
def kasiskidiffsY case"upper":
Y onlylettersY casecase
ctr countsubstringsY
trireps k for kv in ctritems if v
diffs
for tri in trireps:
starts mstart for m in refinditerftri Y
diffs.extendabsxy for xy in combinationsstarts
return nparraysorteddiffs
def indcoX:
X onlylettersX case"upper"
ctr countsubstringsX
n sumctrvalues
return nnsumff for f in ctrvalues
In lecture on D we used an estimate for the Index of Coincidence to distinguish random characters from shift ciphertext. One strength of the Vigenre cipher is that it looks very much like random text from this perspective. Compute indcoY for Y as above, and compare it with which is the value expected for randomly chosen English characters. Comment We've changed indco since Monday. It used to take a dictionary of letter frequencies as input, like mutindco Now it takes a string as input.
On the other hand, the above was encrypted using a Vigenre cipher with a key length of Compute the following and compare the values with the expected Index of Coincidence value for true English of approximately All of the values will be close to but slightly less than, this value. Im not sure if there is a theoretical reason for why they are less than The important thing is they're all significantly closer to the true English value than to the random characters value.
Z onlylettersY # we remove spaces from Y
for i in range:
printindcoZi::
Write a function to perform Vigenre encryption using the following template. If you want to make the code more elegant, replace for i in rangekeylength: in our template with for i k in enumeratekey:
Input: a string X and a list of shift amounts key
Output: a string Y
Example: If X is The rain in Spain stays mainly in the plain" and key is then the output should be the ciphertext written on page of our textbook, beginning YSEDIVTWSDPM
def vigenereX key, case"upper":
X onlylettersX casecase
keylength lenkey
stringlist
for i in rangekeylength:
s # Get the characters in X at integer positions modulo
shifteds # Shift the characters in s Use the imported shiftstring function.
stringlist.appendshifteds
output # Use the imported weave function
return output
Test your function by evaluating the following and checking that the result matches what is on page of the textbook aside from ours being in uppercase letters
X "The rain in Spain stays mainly in the plain"
key
vigenereX key
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