# Arup Guha
# 8/27/2023
# Code for problems 3,4 for CIS 3362 Homework #1

cipher = "sibuovezdgbdfmtuqdwmnhmrgwxuxagsyrgwdfmtuqdwmnhmfeobgszdmmroifggnowmuzxfsfudfgsqfdog"

# modinv[x] is the mod inverse of x mod 26 if it exists, -1 otherwise.
modinv = [-1,1,-1,9,-1,21,-1,15,-1,3,-1,19,-1,-1,-1,7,-1,23,-1,11,-1,5,-1,17,-1,25]

# Possible values of a.
aList = [1,3,5,7,9,11,15,17,19,21,23,25]

# Try all.
for a in aList:
    for b in range(26):

        plain = ""

        # Just append to plain.
        for x in cipher:
            numX = ord(x) - ord('a')
            p = (a*numX + b)%26
            letP = chr(p+ord('a'))
            plain = plain + letP

        # Look for the and and.
        if "the" in plain or "and" in plain:

            # Print possible decryption keys and potential message
            print(a,b,plain)

            # Here is the math to print out the corresponding encryption keys.
            print("encryption keys are a=",modinv[a]," b=",(modinv[a]*(26-b))%26, sep="")

# Question 4 here.
plain = "welcometocryptographyclasshopeyoufindbigprizeswhiledoingyourhomework"

# Just encrypt with hard-coded key given.
for x in plain:
    numX = ord(x) - ord('a')
    c = (11*numX + 8)%26
    print(chr(c+ord('a')),end="")
print()
            
