Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x TypeError:需要类似字节的对象,而不是';int';_Python 3.x - Fatal编程技术网

Python 3.x TypeError:需要类似字节的对象,而不是';int';

Python 3.x TypeError:需要类似字节的对象,而不是';int';,python-3.x,Python 3.x,Hy,我正在尝试python的模块。因此,我制作了一个“加密”(这是非常新的代码)文本并将其加密保存的代码,但是当我试图保存加密的字节时,我得到了一个“TypeError:需要一个类似字节的对象,而不是'int'”。这很奇怪,因为我看到的是“加密的_消息”的类型,它是一个类字节 def encrypt(paswd, formated_data): #generates the salt digest = hashes.Hash(hashes.SHA256(), default_b

Hy,我正在尝试python的模块。因此,我制作了一个“加密”(这是非常新的代码)文本并将其加密保存的代码,但是当我试图保存加密的字节时,我得到了一个“TypeError:需要一个类似字节的对象,而不是'int'”。这很奇怪,因为我看到的是“加密的_消息”的类型,它是一个类字节

def encrypt(paswd, formated_data):
    #generates the salt
    digest = hashes.Hash(hashes.SHA256(), default_backend())
    digest.update(bytes(paswd, "utf-8"))
    salt = digest.finalize()

    kdf = PBKDF2HMAC(
        algorithm = hashes.SHA256,
        length = 32,
        salt = salt,
        iterations = 1000000,
        backend = default_backend()
    )
    key = kdf.derive(bytes(paswd, 'utf-8'))
    iv = os.urandom(16)
    encryptor = Cipher(algorithms.AES(key), modes.CTR(iv), default_backend()).encryptor()
    #untill this line of code everything works fine
    encrypted_msg = encryptor.update(bytes(formated_data, "utf-8")) + encryptor.finalize()

    print(type(encripted_msg)) #this prints <class 'bytes'>

    with open ("./vault/locked.aes", mode = "wb") as f:
        f.writelines( encripted_msg ) #and here i get the error
def加密(paswd,格式化的_数据):
#产生盐
digest=hashes.Hash(hashes.SHA256(),默认值为\u backend())
摘要.更新(字节(paswd,“utf-8”))
salt=digest.finalize()
kdf=PBKDF2HMAC(
算法=hashes.SHA256,
长度=32,
盐=盐,
迭代次数=1000000次,
backend=默认值_backend()
)
key=kdf.derivate(字节(paswd,'utf-8'))
iv=os.Uradom(16)
encryptor=Cipher(algorithms.AES(key)、modes.CTR(iv)、default_backend()).encryptor()
#在这行代码之前,一切正常
encrypted_msg=encryptor.update(字节(格式化的_数据,“utf-8”))+encryptor.finalize()
打印(键入(encripted_msg))#此打印
打开(“./vault/locked.aes”,mode=“wb”)作为f:
f、 writelines(encripted_msg)#这里我得到了错误

顺便说一句,我正在使用Python3.6.8,好的,我找到了解决这个问题的方法。首先,python中的byte类将数据存储为一个列表,如果需要,可以在循环中读取每个字节的数据。因此,二进制数据文件中的方法“writelines()”不使用“\n”来结束每一行,而是将每个字节写入bytes类变量,只需将其转换为一个列表,瞧!,该方法将读取每一咬并将其写入一个文件。

我认为您只需要写入
finalize
的结果,因此
更新
部分应该分开。请制作一个包含完整代码和完整错误消息的回溯。好的,我将用函数的完整代码更新帖子!