#!/usr/bin/env python3
# by @rextco
import base64
from Cryptodome.Cipher import AES

pkcs7_padding = lambda msg, block_size: msg + bytes([block_size - len(msg) % block_size]) * (block_size - len(msg) % block_size)
pkcs7_unpadding = lambda msg: msg[0:-msg[-1]]


def read_file(fn):
    with open(fn, "rt", encoding="utf-8") as content_file:
        c = content_file.read()
    return c


def write_file(fn, text_):
    with open(fn, "wb") as f:
        f.write(text_)


if __name__ == "__main__":
    cipher = AES.new(base64.b64decode("KYNXJ8uBEbjTLfHtqWleRg=="), AES.MODE_CBC, base64.b64decode("7DNnAZNMb2kW7L7Ep38gpg=="))
    ciphertext = base64.b64decode(read_file("encrypted_data.txt"))
    plaintext = pkcs7_unpadding(cipher.decrypt(ciphertext))
    print(plaintext.decode())
    write_file("decipher_data.txt", plaintext)
