@@ -5,3 +5,66 @@ JSON Web Encryption (JWE)
5
5
6
6
JSON Web Encryption (JWE) represents encrypted content using JSON-based data
7
7
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
0 commit comments