Encrypt data in file txt using AES in django

so i want to encrypt data what's in the file i was upload. but when i read the data, the plaintext says it's none (i called that func at func encrypt) but when i called that func at homepage, its works well (can read the data). i don't know what's wrong. please help me to encrypt data in the file txt what i was upload.

here's my code:

def read_file(f):
    f = open('media/txt/'+f, 'r')
    file_content = f.read()

key_bytes = 16

# here's a rudimentary change that accepts bytes or
# str
def encrypt(key, pt):
    plaintext = read_file(pt)
    # plaintext = plaintext[0]
    # if isinstance(plaintext, str):
    #     pt= plaintext.encode("utf-8")

    assert len(key) == key_bytes
    # Choose a random, 16-byte IV.
    iv = Random.new().read(AES.block_size)

    # Convert the IV to a Python integer.
    iv_int = int(binascii.hexlify(iv), 16)

    # Create a new Counter object with IV = iv_int.
    ctr = Counter.new(AES.block_size * 8, initial_value=iv_int)

    # Create AES-CTR cipher.
    aes = AES.new(key.encode('utf8'), AES.MODE_CTR, counter=ctr)

    # Encrypt and return IV and ciphertext.
    ciphertext = aes.encrypt(plaintext)
    return (iv, ciphertext)
Back to Top