def encrypt_to_jwe(content): """Returns a string a serialized encryption from cryptojwt.jwe.jwe_rsa.JWE_RSA """ if isinstance(content, dict): content = json.dumps(content).encode() elif isinstance(content, str): content = content.encode() if not isinstance(content, bytes): raise Exception('encrypt_to_jwe content must be a bytes object') priv_key = import_private_rsa_key_from_file(RSA_KEY) pub_key = priv_key.public_key() _rsa = JWE_RSA(content, alg=JWE_ALG, enc=JWE_ENC) jwe = _rsa.encrypt(pub_key) return jwe
def test_rsa_encrypt_decrypt_rsa_cbc(): _rsa = JWE_RSA(plain, alg="RSA1_5", enc="A128CBC-HS256") jwt = _rsa.encrypt(pub_key) dec = JWE_RSA() msg = dec.decrypt(jwt, priv_key) assert msg == plain
def test_rsa_encrypt_decrypt_rsa_oaep_256_gcm(): jwt = JWE_RSA(plain[:1], alg="RSA-OAEP-256", enc="A256GCM").encrypt(pub_key) msg = JWE_RSA().decrypt(jwt, priv_key) assert msg == plain[:1]
def test_cek_reuse_encryption_rsaes_rsa_oaep(): _rsa = JWE_RSA(plain, alg="RSA-OAEP", enc="A256GCM") jwt = _rsa.encrypt(pub_key) dec = JWE_RSA() msg = dec.decrypt(jwt, priv_key) assert msg == plain _rsa2 = JWE_RSA(plain, alg="RSA-OAEP", enc="A256GCM") jwt = _rsa2.encrypt(None, cek=dec["cek"]) dec2 = JWE_RSA() msg = dec2.decrypt(jwt, None, cek=_rsa["cek"]) assert msg == plain
def test_cek_reuse_encryption_rsaes_rsa15(): _rsa = JWE_RSA(plain, alg="RSA1_5", enc="A128CBC-HS256") jwt = _rsa.encrypt(pub_key) dec = JWE_RSA() msg = dec.decrypt(jwt, priv_key) assert msg == plain _rsa2 = JWE_RSA(plain, alg="RSA1_5", enc="A128CBC-HS256") jwt = _rsa2.encrypt(None, cek=dec["cek"]) dec2 = JWE_RSA() msg = dec2.decrypt(jwt, None, cek=_rsa["cek"]) assert msg == plain