AttributeError:“\u io.TextIOWrapper”对象没有属性“encrypt”

2024-04-28 19:24:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图添加到https://www.thepythoncode.com/article/encrypt-decrypt-files-symmetric-python上列出的一个项目中,这是一个使用python的文本/文件加密设置,但每次我尝试运行代码时,我都会碰到代码中它实际加密文件的部分,我得到上面列出的属性错误

完全错误:

    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'

相关代码:

""" 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)

Tags: pathkeytxttaskdataifaswith
1条回答
网友
1楼 · 发布于 2024-04-28 19:24:35

我在使用Fernet时多次遇到过类似的情况。尝试使用名为 简单的密码。您可以使用pip install simple crypt安装这个库

下面是使用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()

#解密文件中的数据

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)

这对于加密和解密非常简单enter code here

相关问题 更多 >