Python加密解密

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}
'''
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值