即使密钥保持不变,Fernet加密令牌也会发生变化。重复执行下面的示例将显示这一点。
from cryptography.fernet import Fernet
f = Fernet(b'vIkpEFEgCEOSQDfr9cTgDJnOFp9b2Wb7MLv55BhFYYY=')
print(f.encrypt(b'my_dark_secret'))为什么会发生这种情况,我如何确保所产生的令牌保持不变?
发布于 2022-09-04 13:13:45
正如Topcao所指出的,该函数包含一个基于随机+时间的因子。解决方法是使用内部Fernet函数_encrypt_from_parts
from cryptography.fernet import Fernet
f = Fernet(b'vIkpEFEgCEOSQDfr9cTgDJnOFp9b2Wb7MLv55BhFYYY=')
def encrypt(unencrypted_string):
return f._encrypt_from_parts(unencrypted_string.encode(), 0,b'\xbd\xc0,\x16\x87\xd7G\xb5\xe5\xcc\xdb\xf9\x07\xaf\xa0\xfa')
print(encrypt('my dark secret'))https://stackoverflow.com/questions/73599108
复制相似问题