In: Computer Science
Write a python program that implements a Brute Force attack on Shift Cipher. In this program there is only one input - ciphertext - is a sequence of UPPER CASE letters. To make it easy, the program will be interactive and will output all possible plaintexts and ask user which plaintext makes sense. As soon as user will decide YES, the program will stop searching and print the desired plaintext and the found SHIFT KEY.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
#code
#method to shift alphabets of a cipher text to one place
left
def shiftBack(text):
    #creating a string to store shifted
text
    shifted=''
    #looping through each character in
text
    for i in
text:
        #checking if i is an
alphabet
        if
i.isalpha():
           
c=''
           
#if i is 'a', setting c to 'z' (wrapping around)
           
if i=='a':
               
c='z'
           
#doing the same for capital 'A' (setting c to 'Z')
           
elif i=='A':
               
c='Z'
           
else:
               
#ord returns the order-index of a character, chr() takes an
index and
               
#returns the corresponding character, so this statement will assign
c
               
#a value that's before i, for example if i was 'b', then c will be
'a'
               
#if i was 'j', c will be 'i'
          
     c=chr(ord(i)-1)
           
#appending c to shifted
           
shifted+=c
       
else:
           
#all other characters are unchanged, simply appending to
shifted
           
shifted+=i
    #returning shifted text
    return shifted
def main():
    #reading a cipher text (shifted/caesar
text)
    cipher=input('Enter a line of
cipher text: ')
    ch='n' #loop
controller
    shift_key=0 #shift key
    #looping as long as user is not satisfied
    while
ch!='y':
        #updating shift
key
        shift_key+=1
        #shifting back
cipher text once
       
cipher=shiftBack(cipher)
        #printing shifted
text
       
print('\n'+cipher)
        #asking user if this
makes sense
       
ch=input('Does this make sense? (y/n)
').lower()
    #after exiting loop, displaying
shift_key
    print('The shift key
is',shift_key)
#invoking main()
main()
#output
Enter a line of cipher text: olssv dvysk
nkrru cuxrj
Does this make sense? (y/n) n
mjqqt btwqi
Does this make sense? (y/n) n
lipps asvph
Does this make sense? (y/n) n
khoor zruog
Does this make sense? (y/n) n
jgnnq yqtnf
Does this make sense? (y/n) n
ifmmp xpsme
Does this make sense? (y/n) n
hello world
Does this make sense? (y/n) y
The shift key is 7