Option 1 Write a program that accepts ciphertext only. The program finds the key
ID: 3586114 • Letter: O
Question
Option 1 Write a program that accepts ciphertext only. The program finds the keys and decipher the message. To decipher the message use the program you wrote in Lab 1. Use letter frequency analysis and apply known plaintext attack after making assumption on possible plaintext letters.
BELOW IS THE PROGRAM I WROTE IN LAB1
def decryption(a,b,ci):
latters="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
orginal=" "
inv=inverse(a)
for x in range(len(ci)):
c=latters.index(ci[x])
plain=inv*(c-b)%26
orginal+=latters[plain]
return orginal
def encryption(a,b,orginal):
ci=" "
latters="abcdefghijklmnopqrstuvwxyz"
for x in range(len(orginal)):
plain=latters.index(orginal[x])
c=(a*plain+b)%26
ci+=latters[c]
return ci
def inverse(x):
for y in range(0,26):
if (x*y)%26==1:
print(y)
return y
def gcdevisor(x,y):
while y:
x,y=y,x%y
return x
def bruteForce(ci):
latters="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
print("The program will provide random guesses, type N if the guess is wrong and Y if it's correct when prompted.")
for k in range(0,26):
g=" "
for x in range(len(ci)):
c=latters.index(ci[x])
plain=(c-k)%26
g+=latters[plain]
print(g)
t=raw_input("Y/N?:")
if t=='Y':
print("Key:",k)
return g
def main():
print("Welcome to the Affine Cipher!")
print("enter #1 for encryption")
print("enter #2 for decryption")
print("enter #3 for brute Attack")
c=int(input())
if c==1:
orginal=raw_input("Please enter what you want to encrypt (LOWER CASE): ")
a=int(input("Enter key A: "))
b=int(input("Enter key B: "))
if (gcdevisor(a,26)!=1 or b>25):
print("Invalid KEY")
else:
orginal=encryption(a,b,orginal)
print("Encrypted string:")
print(orginal)
elif c==2:
ci=raw_input("Please enter a cipher (UPPER CASE): ")
a=int(input("Enter key A: "))
b=int(input("Enter key B: "))
if (gcdevisor(a,26)!=1 or b>25):
print("Invalid KEY")
else:
ci=decryption(a,b,ci)
print("Decrypted string:")
print(ci)
elif c==3:
ci=raw_input("Please enter a shift cipher (UPPER CASE): ")
ci=bruteForce(ci)
else: print("ERROR")
main()
Explanation / Answer
Hope this programe will help you. And please let me know if i was able to clear you doubt or not..... E(x) = (x + n) % 26 D(x) = (x - n) % 26 def encrypt(n, plaintext): """Encrypt the string and return the ciphertext""" result = '' for l in plaintext.lower(): try: i = (key.index(l) + n) % 26 result += key[i] except ValueError: result += l return result.lower() def decrypt(n, ciphertext): """Decrypt the string and return the plaintext""" result = '' for l in ciphertext: try: i = (key.index(l) - n) % 26 result += key[i] except ValueError: result += l return result def show_result(plaintext, n): """Generate a resulting cipher with elements shown""" encrypted = encrypt(n, plaintext) decrypted = decrypt(n, encrypted) print 'Rotation: %s' % n print 'Plaintext: %s' % plaintext print 'Encrytped: %s' % encrypted print 'Decrytped: %s' % decrypted
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.