Question

In: Computer Science

Write a python program that implements a Brute Force attack on Shift Cipher. In this program...

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.

Solutions

Expert Solution

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


Related Solutions

Write a Python script that performs brute force to extract a password protected zip file named...
Write a Python script that performs brute force to extract a password protected zip file named sec2.zip. The password is believed to be associated with one of the dictionary word in the 'wordlist.txt file. a) Paste your code here b) What is the password?
Write an Java/C program code to perform known-plaintext attack on Permutation Cipher. This program can be...
Write an Java/C program code to perform known-plaintext attack on Permutation Cipher. This program can be used to determine the length of the permutation m and the key permutation.
Write out the “brute-force” (exact) expressions for force and torque on a loop 1 in the...
Write out the “brute-force” (exact) expressions for force and torque on a loop 1 in the magnetic field B of loop 2. Write these expressions using magnetic moment of loop 1. Pay attention to the r, r’, … vectors. A magnet (magnetic dipole) tends to align itself parallel to external field. If the magnetic moment is 5 [CGS units] in -z direction and magnetic field is 25 [CGS units] in z direction. What is the value of the torque? Plot...
Write a program in python that implements quicksort, first using recursion and then without recursion.
Write a program in python that implements quicksort, first using recursion and then without recursion.
please solve the following in python: write a program for the decryption of 2-rail fence cipher....
please solve the following in python: write a program for the decryption of 2-rail fence cipher. For example, the input "Cmhmtmrooeoeoorw" should return "Comehometomorrow", and input "topaesw lyr" should return "two players".
please solve the following in python: write a program for the decryption of 2-rail fence cipher....
please solve the following in python: write a program for the decryption of 2-rail fence cipher. For example, the input "Cmhmtmrooeoeoorw" should return "Comehometomorrow", and input "topaesw lyr" should return "two players".
Write a Python program to implement Vignere Cipher. Take user input to get plain text and...
Write a Python program to implement Vignere Cipher. Take user input to get plain text and key. TRY TO MAKE IT AS EASY AS YOU CAN.
Cryptography and Applications 1. Write Python program to implement Caesar’s Cipher. Take user input to get...
Cryptography and Applications 1. Write Python program to implement Caesar’s Cipher. Take user input to get plain text and key. 2.  Write a Python program to implement Vignere Cipher. Take user input to get plain text and key. TRY TO MAKE IT AS EASY AS YOU CAN.
Cryptography and Applications 1. Write Python program to implement Caesar’s Cipher. Take user input to get...
Cryptography and Applications 1. Write Python program to implement Caesar’s Cipher. Take user input to get plain text and key. 2.  Write a Python program to implement Vignere Cipher. Take user input to get plain text and key. TRY TO MAKE IT AS EASY AS YOU CAN.
Counting evens Write the pseudo-code for a brute force approach to counting the number of even...
Counting evens Write the pseudo-code for a brute force approach to counting the number of even integers in an array of n integers. Write the pseudo-code divide-and-conquer algorithm for counting the number of even integers in an array of n integers. Give the recurrence relation for the number of additions in your divide-and-conquer algorithm.  
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT