JustPaste
HomeCategoriesAboutDonateContactTerms of UsePrivacy Policy
JustPaste

Free online notepad — write and share instantly

Navigate

  • Home
  • Timeline
  • Categories

Info

  • About
  • Donate
  • Contact

Legal

  • Terms of Use
  • Privacy Policy

© 2026 JustPaste.app. All rights reserved.

Made with ♥ by JustPaste

Porg | JustPaste.app
21 days ago6 views
👨‍💻Programming

Porg

3) Program to implement Polyalphabetic Cipher

def generate_key(text, key):
    key = list(key)
    if len(text) == len(key):
        return "".join(key)
    else:
        for i in range(len(text) - len(key)):
            key.append(key[i % len(key)])
    return "".join(key)


def encrypt(text, key):
    cipher = []
    for i in range(len(text)):
        x = (ord(text[i]) + ord(key[i])) % 26
        x += 65
        cipher.append(chr(x))
    return "".join(cipher)


def decrypt(cipher, key):
    plain = []
    for i in range(len(cipher)):
        x = (ord(cipher[i]) - ord(key[i]) + 26) % 26
        x += 65
        plain.append(chr(x))
    return "".join(plain)


text = input("Enter Text: ").upper()
key = input("Enter key: ").upper()

key = generate_key(text, key)

cipher = encrypt(text, key)
print("Encrypted:", cipher)

plain = decrypt(cipher, key)
print("Decrypted:", plain)
← Back to timeline