Multiplicative cipher example

Original Pearto example. Read, copy and adapt this code for your project.

from math import gcd

def encrypt(text, key):
    if gcd(key, 26) != 1:
        raise ValueError("Key must be coprime with 26")
    return "".join(chr(65 + (ord(c) - 65) * key % 26)
                   if "A" <= c <= "Z" else c for c in text.upper())

def decrypt(text, key):
    return encrypt(text, pow(key, -1, 26))

encoded = encrypt("HELLO", 5)
print(encoded, decrypt(encoded, 5))