Poly-Alphabetic Cipher Program in Python Language
Poly-Alphabetic Cipher Program in Python Language
Introduction:
Polyalphabetic ciphers are a type of substitution cipher in which different letters of the plaintext
are enciphered using different substitution alphabets. This makes them more difficult to break
than monoalphabetic ciphers, which use a single substitution alphabet for the entire plaintext.
Algorithm:
The following algorithm describes how to encrypt and decrypt a message using a polyalphabetic
cipher:
Encryption:
1. Generate a random encryption key of the same length as the message.
2. For each character in the message:
● If the character is alphabetic:
○ Shift the character by the ASCII value of the corresponding letter in the
encryption key.
○ Wrap around if necessary.
● Otherwise, leave the character unchanged.
3. The encrypted message is the result of concatenating the shifted characters.
Decryption:
1. For each character in the encrypted message:
● If the character is alphabetic:
○ Shift the character back by the ASCII value of the corresponding letter in
the encryption key.
○ Wrap around if necessary.
● Otherwise, leave the character unchanged.
2. The decrypted message is the result of concatenating the shifted characters.
Implementation of poly-alphabetic cipher:
import random
def poly_alphabetic_cipher(text, key):
encrypted_text = ""
key_length = len(key)
for i in range(len(text)):
char = text[i]
if char.isalpha():
shift = ord(key[i % key_length]) - ord('a')
if char.islower():
encrypted_char = chr(((ord(char) - ord('a') + shift) % 26) + ord('a'))
else:
encrypted_char = chr(((ord(char) - ord('A') + shift) % 26) + ord('A'))
else:
encrypted_char = char
encrypted_text += encrypted_char
return encrypted_text
#User input
message = input("Enter a message: ")
encryption_key = ''.join(random.choice('abcdefghijklmnopqrstuvwxyz') for i in
range(len(message)))
encrypted_message = poly_alphabetic_cipher(message, encryption_key)
print("Encrypted Message:", encrypted_message)
decrypted_message = poly_alphabetic_decipher(encrypted_message, encryption_key)
print("Decrypted Message:", decrypted_message)