0% found this document useful (0 votes)
383 views

Poly-Alphabetic Cipher Program in Python Language

Poly-alphabetic ciphers use multiple substitution alphabets to encrypt different letters of a plaintext message, making them harder to break than monoalphabetic ciphers which use a single alphabet. The algorithm generates a random key the same length as the message, shifts each character by the ASCII value of the corresponding key letter, and concatenates the shifted characters to encrypt or decrypt the message. The implementation takes user input, encrypts the message using a randomly generated key, then decrypts it with the same key to test the cipher.

Uploaded by

batch22ubit
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
383 views

Poly-Alphabetic Cipher Program in Python Language

Poly-alphabetic ciphers use multiple substitution alphabets to encrypt different letters of a plaintext message, making them harder to break than monoalphabetic ciphers which use a single alphabet. The algorithm generates a random key the same length as the message, shifts each character by the ASCII value of the corresponding key letter, and concatenates the shifted characters to encrypt or decrypt the message. The implementation takes user input, encrypts the message using a randomly generated key, then decrypts it with the same key to test the cipher.

Uploaded by

batch22ubit
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Poly-Alphabetic Cipher

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

def poly_alphabetic_decipher(encrypted_text, key):


decrypted_text = ""
key_length = len(key)
for i in range(len(encrypted_text)):
char = encrypted_text[i]
if char.isalpha():
shift = ord(key[i % key_length]) - ord('a')
if char.islower():
decrypted_char = chr(((ord(char) - ord('a') - shift) % 26) + ord('a'))
else:
decrypted_char = chr(((ord(char) - ord('A') - shift) % 26) + ord('A'))
else:
decrypted_char = char
decrypted_text += decrypted_char
return decrypted_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)

You might also like