# Arup Guha
# 8/20/2025
# Applies Affine Cipher to a String

# Takes in the text and keys a and b, and applies them to the text
def affine(plain,a,b):

    # Store result here.
    res = ""

    # Go through each letter.
    for x in plain:

        # Numeric value of ciphertext letter 0 - 25
        cipherNum = (a*(ord(x)-ord('a'))+b)%26

        # Map to character and add to result.
        res = res + chr(cipherNum+ord('a'))
    return res

# Get message.
msg = input("Enter a lowercase letter message to encrypt.\n")

# Encrypt.
cipher = affine(msg,3,5)
print(cipher)

# Decrypt
getBack = affine(cipher,9,7)
print(getBack)

''' Test at end of class checking if our supposed decrypt key works
Our matching info was cipher(M) --> plain(B) and cipher(Q) --> plain(H)
When we solved the system, we got a = 21, b = 9.
Here is the ciphertext: wzuynzooxmbqheex
'''
# Encrypt.
plain = affine("wzuynzooxmbqheex",21,9)
print(plain)

