我正在使用jwcrypto加密数据使用公钥。我已经看过了文档,JWE类只将明文作为有效负载。
但我有字典要加密作为有效载荷。
我可以将字典转换为json并加密有效负载,但是解密我的数据的人将在解密后期待字典。
不管怎样,我可以加密字典作为有效载荷。
发布于 2019-10-25 09:55:51
JWE定义了一种对JSON友好的方法来加密任意数据。
所以,您想要的不是JWE,而是JWT令牌(加密python字典,它映射到JSON对象)。JWT基本上是使用JWS和JWE标准对JSON对象进行签名和/或加密。
只需使用jwcrypto的JWT部分:https://jwcrypto.readthedocs.io/en/latest/jwt.html
应该是这样的:
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 = jwt.serialize()然后,必须使用假定令牌是JWT的库来进行反序列化,否则您实际上将得到JSON有效负载的字符串表示,您将不得不将自己解码为Python。
https://stackoverflow.com/questions/58555492
复制相似问题