0
votes

Im trying to add on to a project listed on https://www.thepythoncode.com/article/encrypt-decrypt-files-symmetric-python which is a text/file encryption setup with python, but every time I try to run the code, I hit the part of the code where it is actually encrypting the file and I'm getting the attribute error listed above

Full Error:

    Traceback (most recent call last):
    File "/Users/--------/Desktop/CODE/Python/TFEncrypter/TFEncrypter.py", line 38, in 
    <module>
    encrypted_data = f.encrypt(file_data)
    AttributeError: '_io.TextIOWrapper' object has no attribute 'encrypt'

Relevant Code:

""" IMPORTING AND DEFINITIONS """

import os
from cryptography.fernet import Fernet
def write_key():
    key = Fernet.generate_key()
    with open("key.tfe", "wb") as key_file:
        key_file.write(key)

def load_key():
    return open("key.tfe", "rb").read()

def make_file():
    open("tte.txt", "x")

def encrypt(filename, key):
    f = Fernet(key)



""" START OF PROGRAM """

path="key.tfe"
if os.path.isfile(path):
    load_key()
    task = input("Would You Like To Encrypt Or Decrypt A File?")
    if type(task) == str:
        if task == "Encrypt" or "encrypt":
            task = input("Would You Like To Create A New File To Encrypt, Or Encrypt A Pre-Existing File (Note: Pre-Existing Files Must Be Named tte.txt) ANSWER AS: 'NEW FILE' or 'OLD FILE'")
            if task == "NEW FILE":
                path="tte.txt"
                if os.path.isfile(path):
                    towrite = input("Text to encrypt in file:")
                    f = open("tte.txt", "w")
                    f.write(towrite)
                    with open("tte.txt", "rb") as file:
                        file_data = file.read()
                    encrypted_data = f.encrypt(file_data)
                    with open("encrypted.tfe", "wb") as file:
                        file.write(encrypted_data)
                else:
                    make_file()
                    towrite = input("Text to encrypt in file:")
                    f = open("tte.txt", "w")
                    f.write(towrite)
                    with open("tte.txt", "rb") as file:
                        file_data = file.read()
                    encrypted_data = f.encrypt(file_data)
                    with open("encrypted.tfe", "wb") as file:
                        file.write(encrypted_data)
1

1 Answers

1
votes

I have faced similar experience many times using Fernet. Try with simple library called simplecrypt. You can install this library using pip install simple-crypt

Below is one example for the usage of the simplecrypt

    import pandas as pd
    from io import StringIO
    from simplecrypt import encrypt, decrypt

    #Create a dataframe
    data = {'Date': ['2019-10-10', '2019-10-12', '2019-10-15'], \
            'Credit Amount (SGD)': [40.00, 500.00, 60.00],\
            'Transaction type': ['CC *1234', 'Bank transfer', 'CC *1234']}
    df = pd.DataFrame(data)

    #store in csv format
    data_csv = df.to_csv(index=False)

    #Encrypt the data by giving password
    encdata = encrypt("provide password", data_csv)

    #Store encrypted data in file
    file_w = open('file.enc', 'wb')
    file_w.write(encdata)
    file_w.close()

#Decrypt the data from the file

    file_r = open('file.enc','rb').read()
    CSVplaintext = decrypt("provide the same password", file_r).decode('utf8')
    DATA=StringIO(CSVplaintext)

    df = pd.read_csv(DATA)

This is very simple to use for encryption and decryptionenter code here