Question: From a Vignere cipher, please complete the following Python code to return the key used to encrypt a message. Verify that it works with messages
From a Vignere cipher, please complete the following Python code to return the key used to encrypt a message. Verify that it works with messages encrypted with the following keys: ucla, ece, ijl, gubf, chxbz, xlmviu
from collections import Counter
# This function returns the
# encrypted text generated
# with the help of the key
def cipherText(string, key):
cipher_text = []
for i in range(len(string)):
x = (ord(string[i]) +
ord(key[i])) % 26
x += ord('A')
cipher_text.append(chr(x))
return("" . join(cipher_text))
# This function decrypts the
# encrypted text and returns
# the original text
def originalText(cipher_text, key):
orig_text = []
for i in range(len(cipher_text)):
x = (ord(cipher_text[i]) -
ord(key[i]) + 26) % 26
x += ord('A')
orig_text.append(chr(x))
return("" . join(orig_text))
# This method can be used to calculate the Index of Coincidence.
# Note that a text written in English language has an index of coincidence of 0.0667
def IoC(self, text):
text_length = 0
letterCounts = Counter(text)
total = 0
for ni in letterCounts.values():
total += ni * (ni-1)
text_length += ni
if text_length == 0:
return 0
else:
return total / (text_length * (text_length-1))
#please complete this method using the IoC function
def getKey(self, ciphertext) -> str:
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
