Skip to content

Commit e336831

Browse files
committed
Basic JWE doc
1 parent af22f92 commit e336831

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

doc/jwe.rst

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,66 @@ JSON Web Encryption (JWE)
55

66
JSON Web Encryption (JWE) represents encrypted content using JSON-based data
77
structures.
8+
9+
It's assumed that you know all you need to know about key handling if not
10+
please spend some time reading keyhandling_ .
11+
12+
When it comes to JWE there are basically 2 things you want to be able to do:
13+
encrypt some data and decrypt some encrypted data. I'll deal with
14+
them in that order.
15+
16+
Encrypting a document
17+
---------------------
18+
19+
This is the high level way of doing things.
20+
There are few steps you have to go through. Let us start with an example and then break it into its parts::
21+
22+
>>> from cryptojwt.jwk.rsa import RSAKey
23+
>>> from cryptojwt.jwe.jwe import JWE
24+
25+
>>> priv_key = import_private_rsa_key_from_file(KEY)
26+
>>> pub_key = priv_key.public_key()
27+
>>> encryption_key = RSAKey(use="enc", pub_key=pub_key, kid="some-key-id")
28+
>>> plain = b'Now is the time for all good men to come to the aid of ...'
29+
>>> encryptor = JWE(plain, alg="RSA-OAEP", enc="A256CBC-HS512")
30+
>>> jwe = encryptor.encrypt(keys=[encryption_key], kid="some-key-id")
31+
32+
The steps:
33+
34+
1. You need an encryption key. The key *MUST* be instances of
35+
:py:class:`cryptojwt.jwk.JWK`.
36+
2. You need the information that are to be signed. It must be in the form of a string.
37+
3. You initiate the encryptor, provide it with the message and other
38+
needed information.
39+
4. And then you encrypt as described in RFC7516_ .
40+
41+
There is a lower level way of doing the same it will look like this::
42+
43+
>>> from cryptojwt.jwk.rsa import import_private_rsa_key_from_file
44+
>>> from cryptojwt.jwe.jwe_rsa import JWE_RSA
45+
46+
>>> priv_key = import_private_rsa_key_from_file(KEY)
47+
>>> pub_key = priv_key.public_key()
48+
>>> plain = b'Now is the time for all good men to come to the aid of ...'
49+
>>> _rsa = JWE_RSA(plain, alg="RSA1_5", enc="A128CBC-HS256")
50+
>>> jwe = _rsa.encrypt(pub_key)
51+
52+
Here the key is an cryptography.hazmat.primitives.asymmetric.rsa.RSAPrivateKey
53+
instance and the encryptor is a :py:class:`cryptojwt.jwe.jew_rsa.JWE_RSA`
54+
instance.
55+
56+
Decrypting something encrypted
57+
------------------------------
58+
59+
Decrypting using the encrypted message above.
60+
61+
>>> from cryptojwt.jwe.jwe import factory
62+
>>> from cryptojwt.jwk.rsa import RSAKey
63+
64+
>>> _decryptor = factory(jwt, alg="RSA1_5", enc="A128CBC-HS256")
65+
>>> _dkey = RSAKey(priv_key=priv_key)
66+
>>> msg = _decryptor.decrypt(jwe, [_dkey])
67+
68+
69+
70+
.. _RFC7516: https://tools.ietf.org/html/rfc7516

tests/test_07_jwe.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,14 +271,12 @@ def test_rsa_encrypt_decrypt_rsa_oaep_256_gcm():
271271

272272
def test_encrypt_decrypt_rsa_cbc():
273273
_key = RSAKey(pub_key=pub_key)
274-
_key._keytype = "public"
275274
_jwe0 = JWE(plain, alg="RSA1_5", enc="A128CBC-HS256")
276275

277276
jwt = _jwe0.encrypt([_key])
278277

279278
_jwe1 = factory(jwt, alg="RSA1_5", enc="A128CBC-HS256")
280279
_dkey = RSAKey(priv_key=priv_key)
281-
_dkey._keytype = "private"
282280
msg = _jwe1.decrypt(jwt, [_dkey])
283281

284282
assert msg == plain

0 commit comments

Comments
 (0)