Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

using python Write a program that automatically breaks a shift cipher using the

ID: 3810213 • Letter: U

Question

using python Write a program that automatically breaks a shift cipher using the following simple heuristic: the letter that occurs most frequently in the ciphertext corresponds to plaintext 'e' (which, at 12.5% is the most frequent letter in English). Write a function simpleshiftbreak(ct) that given ciphertext ct, returns the key suggested by this heuristic. You can make use of the shift-cipher program shift.py we saw in class.

shift.py

def shift2(pt,k):
'plaintext pt, shift k'
ptlst = encode(pt)
ctlst = [(x+k) % 26 for x in ptlst]
return decode(ctlst)

Explanation / Answer

Please pay attention to indentation.

import collections
#above import is required to find the most frequent character

def simpleshiftbreak(ct):
temp=collections.Counter(ct).most_common(1)[0]
return (ord(temp[0])-ord('e'))%26

#temp[0] will have the most frequent character
#ord('a') tells us the ascii value of a
#we are assuming that all the input string is in lower case

#we have used %26 because our key should not be negative

ct='aaaaaaaaassssssssssbbbbbbxxxxxxxxxxxascvbbbbbbbbbbbbbbbxxxxxxxxxxcccccdasdadfakgjd'

print(str(simpleshiftbreak(ct)))

#our output is: 23