以下代码适用于我:
Python 3.8.3 (default, May 19 2020, 06:50:17) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import cryptography.fernet as cpf
>>> key = cpf.Fernet.generate_key()
>>> f = cpf.Fernet(key)
>>> m = f.encrypt("1234, abcd, '123CE'".encode())
>>> m
b'gAAAAABgbsGYBZjqDDilTYl5zmBfENlPkEbEdnK242_K1IBovuL-oiGrvvZmUZZMUoMQ2jYUdJ32GaI834U_Oe5Bdxhf6r-hIG5KwFawfmN45ewjSlgmzx4='
但是,生成字符串时可能会出现问题:
>>> s = "my string" + b"my bytestring"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "bytes") to str
>>> s = "my string" + b"my bytestring".decode()
>>> s
'my stringmy bytestring'
在尝试连接它们之前,请确保将所有字符串转换为任一str
或格式。bytestring
为此,请使用:
s.decode()
转换s
为str
ifs
是bytestring
s.encode()
转换s
为bytestring
ifs
是str
类型。