import base64
from Cryptodome.Cipher import AES
from Cryptodome.Random import get_random_bytes
         
#Encryption function
def EncryptString(text, phrase):
    phrase_len = 32
    while len(phrase) < phrase_len:
        phrase += phrase
    phrase = phrase[0:phrase_len]
    pad = lambda s: s + (phrase_len - len(s) % phrase_len) * chr(phrase_len - len(s) % phrase_len)
    key = phrase
    IV = get_random_bytes(AES.block_size)

    cryptor = AES.new(key.encode("utf8"), AES.MODE_CBC, IV)
    ciphertext = cryptor.encrypt(bytes(pad(text), encoding="utf8"))
    return base64.b64encode(IV + ciphertext).decode()

#Decryption function
def DecryptString(text, phrase):
    phrase_len = 32
    while len(phrase) < phrase_len:
        phrase += phrase
    phrase = phrase[0:phrase_len]
    key = phrase
    unpad = lambda s: s[0:-ord(s[-1:])]
    plain_text = base64.b64decode(text)
    encoded_data = plain_text[AES.block_size:]
    IV = plain_text[0:AES.block_size]

    cryptor = AES.new(key.encode("utf8"), AES.MODE_CBC, IV)
    decrypted = cryptor.decrypt(encoded_data)
    return unpad(decrypted).decode()
 

string_plano = "Soluciones Andinas"
secret_key = "MUN.G.BAIGORRIATASAPC_7ce56a47-6718-41a4-8677-52fe438d4f7b"
e = EncryptString(string_plano, secret_key)
d = DecryptString(e, secret_key)
print("String.......: " + string_plano)
print("Encriptado...: " + e)
print("Desencriptado: " + d)