0% found this document useful (0 votes)
13 views3 pages

Cssexp

The document contains Python functions for three types of ciphers: Additive, Multiplicative, and Autokey. Each cipher has corresponding encryption and decryption functions, allowing users to input plaintext and keys to generate encrypted text and then decrypt it back to the original plaintext. A main function provides a user interface to choose the cipher type and input the necessary data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Cssexp

The document contains Python functions for three types of ciphers: Additive, Multiplicative, and Autokey. Each cipher has corresponding encryption and decryption functions, allowing users to input plaintext and keys to generate encrypted text and then decrypt it back to the original plaintext. A main function provides a user interface to choose the cipher type and input the necessary data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

def additive_cipher(plaintext, key):

cipher_text = ""
for char in plaintext:
if char.isalpha():
shift = key % 26
if char.islower():
cipher_text += chr((ord(char) - ord('a') + shift) % 26 + ord('a'))
elif char.isupper():
cipher_text += chr((ord(char) - ord('A') + shift) % 26 + ord('A'))
else:
cipher_text += char
return cipher_text

def decrypt_additive_cipher(ciphertext, key):


plain_text = ""
for char in ciphertext:
if char.isalpha():
shift = key % 26
if char.islower():
plain_text += chr((ord(char) - ord('a') - shift) % 26 + ord('a'))
elif char.isupper():
plain_text += chr((ord(char) - ord('A') - shift) % 26 + ord('A'))
else:
plain_text += char
return plain_text

def multiplicative_cipher(plaintext, key1, key2):


cipher_text = ""
for char in plaintext:
if char.isalpha():
shift = key1 * key2 % 26
if char.islower():
cipher_text += chr((ord(char) - ord('a') + shift) % 26 + ord('a'))
elif char.isupper():
cipher_text += chr((ord(char) - ord('A') + shift) % 26 + ord('A'))
else:
cipher_text += char
return cipher_text

def decrypt_multiplicative_cipher(ciphertext, key1, key2):


plain_text = ""
for char in ciphertext:
if char.isalpha():
shift = key1 * key2 % 26
if char.islower():
plain_text += chr((ord(char) - ord('a') - shift) % 26 + ord('a'))
elif char.isupper():
plain_text += chr((ord(char) - ord('A') - shift) % 26 + ord('A'))
else:
plain_text += char
return plain_text

def autokey_cipher(plaintext, key):


cipher_text = ""
key_extended = key + plaintext
for i in range(len(plaintext)):
if plaintext[i].isalpha():
shift = ord(key_extended[i].lower()) - ord('a')
if plaintext[i].islower():
cipher_text += chr((ord(plaintext[i]) - ord('a') + shift) % 26 +
ord('a'))
elif plaintext[i].isupper():
cipher_text += chr((ord(plaintext[i]) - ord('A') + shift) % 26 +
ord('A'))
else:
cipher_text += plaintext[i]
return cipher_text

def decrypt_autokey_cipher(ciphertext, key):


plain_text = ""
key_extended = key + ciphertext
for i in range(len(ciphertext)):
if ciphertext[i].isalpha():
shift = ord(key_extended[i].lower()) - ord('a')
if ciphertext[i].islower():
plain_text += chr((ord(ciphertext[i]) - ord('a') - shift) % 26 +
ord('a'))
elif ciphertext[i].isupper():
plain_text += chr((ord(ciphertext[i]) - ord('A') - shift) % 26 +
ord('A'))
else:
plain_text += ciphertext[i]
return plain_text

def main():
print("Choose Cipher Type:")
print("1. Additive Cipher")
print("2. Multiplicative Cipher")
print("3. Autokey Cipher")

cipher_choice = int(input("Enter choice (1/2/3): "))


plaintext = input("Enter the plaintext: ")

if cipher_choice == 1:
key = int(input("Enter the additive key (integer): "))
encrypted_text = additive_cipher(plaintext, key)
decrypted_text = decrypt_additive_cipher(encrypted_text, key)
print(f"\nPlaintext: {plaintext}")
print(f"Encryption (Additive Cipher) Text: {encrypted_text}")
print(f"Decryption (Additive Cipher) Text: {decrypted_text}")
print(f"Key: {key}")

elif cipher_choice == 2:
key1, key2 = 7, 15
encrypted_text = multiplicative_cipher(plaintext, key1, key2)
decrypted_text = decrypt_multiplicative_cipher(encrypted_text, key1, key2)
print(f"\nPlaintext: {plaintext}")
print(f"Encryption (Multiplicative Cipher) Text: {encrypted_text}")
print(f"Decryption (Multiplicative Cipher) Text: {decrypted_text}")
print(f"Key pair: ({key1}, {key2})")

elif cipher_choice == 3:
key = input("Enter the autokey (string): ")
encrypted_text = autokey_cipher(plaintext, key)
decrypted_text = decrypt_autokey_cipher(encrypted_text, key)
print(f"\nPlaintext: {plaintext}")
print(f"Encryption (Autokey Cipher) Text: {encrypted_text}")
print(f"Decryption (Autokey Cipher) Text: {decrypted_text}")
print(f"Key: {key}")

else:
print("Invalid choice, please try again.")

if __name__ == "__main__":
main()

OUTPUT-

Enter choice (1/2/3): 1


Enter the plaintext: hello
Enter the additive key (integer): 3
Enter choice (1/2/3): 1
Enter the plaintext: hello
Enter the additive key (integer): 3

Plaintext: hello
Encryption (Additive Cipher) Text: khoor
Decryption (Additive Cipher) Text: hello
Key: 3

Enter choice (1/2/3): 2


Enter the plaintext: goodbye

Plaintext: goodbye
Encryption (Multiplicative Cipher) Text: oyhhfli
Decryption (Multiplicative Cipher) Text: goodbye
Key pair: (7, 15)

Enter choice (1/2/3): 3


Enter the plaintext: world
Enter the autokey (string): key

Plaintext: world
Encryption (Autokey Cipher) Text: xqpvi
Decryption (Autokey Cipher) Text: world
Key: key

You might also like