Python高级
Python高级-itsdangerous加密,解密
Python高级-itsdangerous加密,解密
itsdangerous加密解密的两种方法
itsdangerous的两种方法:JSONWebSignatureSerializer,TimedJSONWebSignatureSerializer
JSONWebSignatureSerializer不能设置加密解密的有效时间
TimedJSONWebSignatureSerializer 可以设置加密解密的有效时间
dumps():加密
loads(): 解密
JSONWebSignatureSerializer 代码实例:
from itsdangerous.jws import JSONWebSignatureSerializer key_time = JSONWebSignatureSerializer(secret_key='helloworld') # 没有失效时间 encryption = key_time.dumps({"age":13}) # encryption加密字典 # encryption = key_time.dumps('aaaa') # encryption加密字符串 # encryption = key_time.dumps(['a','b','c']) # encryption加密列表 # encryption = key_time.dumps(('a','b','c')) # encryption加密元组 print(type(encryption)) # <class 'bytes'> print(encryption) print(type(encryption.decode())) # 转化为字符串 print(encryption.decode()) deciphering = key_time.loads(encryption) # encryption 解密 print(type(deciphering)) # 加密元组的话,解密出来的是列表 print(deciphering) ''' 运行结果 <class 'bytes'> b'eyJhbGciOiJIUzUxMiJ9.eyJhZ2UiOjEzfQ.Br-uO_xaS-TLfUnxoG8a6PPcBWmvzfqp_AYuMhET5Oxi5dpbnLw399ZSTv9CIKEqspkszP6GcTYCdJCKnNNoBQ' <class 'str'> eyJhbGciOiJIUzUxMiJ9.eyJhZ2UiOjEzfQ.Br-uO_xaS-TLfUnxoG8a6PPcBWmvzfqp_AYuMhET5Oxi5dpbnLw399ZSTv9CIKEqspkszP6GcTYCdJCKnNNoBQ <class 'dict'> {'age': 13} '''
TimedJSONWebSignatureSerializer代码实例:
from itsdangerous.jws import TimedJSONWebSignatureSerializer key_time = TimedJSONWebSignatureSerializer(secret_key='helloworld',expires_in=300) # 失效时间是300秒 encryption = key_time.dumps({"age":13}) # encryption加密字典 # encryption = key_time.dumps('aaaa') # encryption加密字符串 # encryption = key_time.dumps(['a','b','c']) # encryption加密列表 # encryption = key_time.dumps(('a','b','c')) # encryption加密元组 print(type(encryption)) # <class 'bytes'> print(encryption) print(type(encryption.decode())) # 转化为字符串类型 print(encryption.decode()) deciphering = key_time.loads(encryption) # encryption 解密 print(type(deciphering)) # 加密元组的话,解密出来的是列表 print(deciphering) ''' 运行结果 <class 'bytes'> b'eyJpYXQiOjE1ODM2NTg1MzksImFsZyI6IkhTNTEyIiwiZXhwIjoxNTgzNjU4NTY5fQ.eyJhZ2UiOjEzfQ.gIfHAc7qwIavLNf8bm0TVz7jTu3o9uD7-OfwKCazRLZlIdVvwTy6LQiIfi_b3UqX9M-6XAwCXwSvZVIGjoIkiA' <class 'str'> eyJpYXQiOjE1ODM2NTg1MzksImFsZyI6IkhTNTEyIiwiZXhwIjoxNTgzNjU4NTY5fQ.eyJhZ2UiOjEzfQ.gIfHAc7qwIavLNf8bm0TVz7jTu3o9uD7-OfwKCazRLZlIdVvwTy6LQiIfi_b3UqX9M-6XAwCXwSvZVIGjoIkiA <class 'dict'> {'age': 13} '''
最后修改:2020年3月8日 16:58