import json, base64
alphabet = {"A": 1, "B": 2, "C":3, "D":4, "E":5, "F":6, "G":7,"H": 8,"I":9,"J":10,"K":11,"L":12,"M":13,"N":14,"O":15,"P":16,"Q":17,"R":18,"S":19,"T":20,"U":21,"V":22,"W":23,"X":24,"Y":25,"Z":26," ":27}

#you can enter any two prime numbers here - the bigger the better!
prime1 = 241
prime2 = 389

modulus = prime1 * prime2
totient = (prime1-1) * (prime2-1)

#can be any prime number less than the ones given above
public = 29

#now find the private key via 'brute force'
for i in range(totient):
    if ((1+totient*i)/public % 1) == 0:
        private = int((1+totient*i)/public)
        break

def decrypt(input, d, n):
    return input**d % n

def decrypt_text(input):
    input = json.loads(base64.b64decode(input))
    output = []
    for char in input:
        character_result = decrypt(char, private, modulus)
        for character, num in alphabet.items():
            if character_result == num:
                output.append(character)
    return output

print("Your public key: ",public)

input = input("Enter encrypted message: ")

text = ''.join(decrypt_text(input))
print(text)
