from jwcrypto.jwt import JWT
from jwcrypto.jwk import JWK
claims = {"my": "claims"} # your claims as a Python dict, that can be JSON-encoded
key = JWK.generate(kty='EC').public() # this generates an EC key, you must replace that with your recipient public key
jwt = JWT(header={"alg": "ECDH-ES+A128KW", "enc": "A256CBC-HS512"}, claims=claims) # set your own alg here according to your needs
jwt.make_encrypted_token(key)
serialized_jwt = token.serialize()
JWE定义了一种JSON友好的方式来加密任意数据。你知道吗
所以您想要的(加密python字典,它映射到JSON对象)不是JWE,而是JWT令牌。JWT基本上使用JWS和JWE标准对JSON对象进行签名和/或加密。你知道吗
只需使用jwcrypto doc的JWT部分:https://jwcrypto.readthedocs.io/en/latest/jwt.html
应该是这样的:
然后,反序列化必须通过一个库来完成,假设该令牌是JWT,否则您将得到JSON负载的字符串表示,您将不得不将自己解码为Python dict
相关问题 更多 >
编程相关推荐