#!/usr/bin/env python3
# coding: utf-8

import base64
from Crypto.Cipher import AES

def encrypt_aes_256(clear_text, key, iv):
    key_byte = key.encode('utf-8')
    key_byte = key_byte.ljust(32, "\0".encode('utf-8'))
    if len(key_byte) > 32:
        key_byte = key_byte[:32]
    iv_byte = iv.encode('utf-8')
    iv_byte = iv_byte.ljust(16, "\0".encode('utf-8'))
    if len(iv_byte) > 16:
        key_byte = key_byte[:16]

    # PKCS#5
    pad_len = 16 - len(clear_text) % 16
    padding = chr(pad_len) * pad_len
    clear_text += padding

    cryptor = AES.new(key_byte, AES.MODE_CBC, iv_byte)
    data = cryptor.encrypt(clear_text)

    return base64.b64encode(data).decode('utf-8')

def decrypt_aes_256(data, key, iv):
    data_byte = base64.b64decode(data.encode('utf-8'))
    key_byte = key.encode('utf-8')
    key_byte = key_byte.ljust(32, "\0".encode('utf-8'))
    if len(key_byte) > 32:
        key_byte = key_byte[:32]
    iv_byte = iv.encode('utf-8')
    iv_byte = iv_byte.ljust(16, "\0".encode('utf-8'))
    if len(iv_byte) > 16:
        key_byte = key_byte[:16]

    cryptor = AES.new(key_byte, AES.MODE_CBC, iv_byte)
    c_text = cryptor.decrypt(data_byte)

    # PKCS#5
    pad_len = ord(c_text.decode('utf-8')[-1])
    clear_text = c_text.decode('utf-8')[:-pad_len]

    return clear_text

def main():
    clear_text = "BSプレミアム20日放送"
    key = "SecretKey"
    iv = "InitVector"
    data = encrypt_aes_256(clear_text, key, iv)

    print("Encrypted String: " + data)
    print(decrypt_aes_256(data, key, iv))

if __name__ == "__main__":
    main()


TEST COMMAND-
echo -n "BSプレミアム20日放送" | openssl enc -e -aes-256-cbc -K "5365637265744B65790000000000000000000000000000000000000000000000" -iv "496E6974566563746F72000000000000" -nosalt -a

echo "uatdkMyXtxSIgbvVthf3mYfYbyAOkZDYy+/eGbw7ukA=" | openssl enc -d -aes-256-cbc -K "5365637265744B65790000000000000000000000000000000000000000000000" -iv "496E6974566563746F72000000000000" -nosalt -a
