# Arup Guha
# 11/9/2024
# Example of the Vigenere Cipher

# Encrypts plain using the key key for the Vigenere Cipher.
def encrypt(plain, key):

    # Put answer here.
    cipher = ""
    sz = len(key)

    # Loop  through plain text.
    for i in range(len(plain)):

        # Here we add, use mod to access correct letter in key.
        let = (ord(plain[i]) - ord('A') + ord(key[i%sz]) - ord('A'))%26
        cipher = cipher + chr(let+ord('A'))

    return cipher

# Encrypts cipher using the key key for the Vigenere Cipher.
def decrypt(cipher, key):

    # Put answer here.
    plain = ""
    sz = len(key)

    # Loop through letters.
    for i in range(len(cipher)):

        # Here we subtract. Make note of use of ord('A') necessary to convert
        # from Ascii to 0-25.
        let = (ord(cipher[i]) - ord('A') - (ord(key[i%sz]) - ord('A')) + 26)%26
        plain = plain + chr(let+ord('A'))

    return plain

# Test.
print(encrypt("SENDMORE", "ACM"))
print(decrypt("SGZDOARG", "ACM"))
