Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In python create a program that will encrypt and decrypt using an affine cipher using the formula e(m) k1 * m + k2 (mod p),

In python create a program that will encrypt and decrypt using an affine cipher using the formula e(m) k1 * m + k2 (mod p), d(c) k'1 * (c-k2) (mod p). The program must need to be able to encrypt or decrypt numbers or letters using k1,k2, mod p, m for plaintext and c for cipher text. Example: k= (34,71) p = 541 m = 204 or c = 431. The program must allow the user to enter the plaintext or cipher text and output either the encrypted or decrypted text.

def encrypt(plaintext, a, b):

ciphertext = ""

for char in plaintext:

if char.isalpha():

shift = ord(char.upper())-65

ciphertext += chr(((a*shift + b) % 26) + 65)

else:

ciphertext += char

return ciphertext

def decrypt(ciphertext, a, b):

plaintext = ""

a_inv = 0

for i in range(26):

if (a * i) % 26 == 1:

a_inv = i

for char in ciphertext:

if char.isalpha():

shift = ord(char.upper())-65

plaintext += chr(((a_inv * (shift - b)) % 26) + 65)

else:

plaintext += char

return plaintext

def main():

while True:

print("1. Encrypt")

print("2. Decrypt")

print("3. Quit")

choice = int(input("Enter your choice: "))

if choice == 1:

plaintext = input("Enter plaintext: ")

a = int(input("Enter a: "))

b = int(input("Enter b: "))

print("Ciphertext: " + encrypt(plaintext, a, b))

elif choice == 2:

ciphertext = input("Enter ciphertext: ")

a = int(input("Enter a: "))

b = int(input("Enter b: "))

print("Plaintext: " + decrypt(ciphertext, a, b))

elif choice == 3:

break

else:

print("Invalid option selected.")

if __name__ == "__main__":

main()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database Administration The Essential Reference

Authors: Brian Laskey, David Kreines

1st Edition

1565925165, 978-1565925168

More Books

Students also viewed these Databases questions

Question

8. Set goals that relate to practice as well as competition.

Answered: 1 week ago