JWT encrypting payload in python? (JWE)

前端 未结 2 2102
情歌与酒
情歌与酒 2020-12-20 16:34

According to RFC 7516 it should be possible to encrypt the payload/claim, called JWE.

Are there any python libraries out there that support that?

I\'ve check

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-20 17:27

    Both Jose and jwcrypto libraries can do JWE.

    For jose:

    claims = {
    'iss': 'http://www.example.com',
    'sub': 42,
    }
    pubKey = {'k':\
               '-----BEGIN PUBLIC KEY-----\n\
    -----END PUBLIC KEY-----'
        }
    # decrypt on the other end using the private key
    privKey = {'k': 
        '-----BEGIN RSA PRIVATE KEY-----\n'+\
    '-----END RSA PRIVATE KEY-----'
    }
    
    encJwt = jose.encrypt(claims, pubKey)
    serJwt = jose.serialize_compact(encJwt)
    decJwt = jose.decrypt(jose.deserialize_compact(serJwt), privKey)
    

    For jwcrypto:

    # algorithm to use
    eprot = {'alg': "RSA-OAEP", 'enc': "A128CBC-HS256"}
    stringPayload = u'attack at dawn'
    E = jwe.JWE(stringPayload, json_encode(eprot))
    E.add_recipient(pubKey)
    encrypted_token = E.serialize(compact=True)
    E = jwe.JWE()
    E.deserialize(encrypted_token, key=privKey)
    decrypted_payload = E.payload
    

提交回复
热议问题