forked from Montspy/LooPyGen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjwe-experiment.py
More file actions
executable file
·68 lines (54 loc) · 1.71 KB
/
jwe-experiment.py
File metadata and controls
executable file
·68 lines (54 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from pprint import pprint
from jose import jwe
import json
import os
def encrypt_config(config, secret):
# Derive key
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
salt = os.urandom(16)
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=390000,
)
key = kdf.derive(secret)
# Encrypt
cypher = jwe.encrypt(json.dumps(config), key, algorithm="dir", encryption="A256GCM")
enc_config = {
"cypher": cypher.decode("utf-8"),
"salt": "0x" + salt.hex()
}
return enc_config
def decrypt_config(enc_config, secret):
# Derive key
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
salt = bytes.fromhex(enc_config['salt'].replace("0x", ""))
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=390000,
)
key = kdf.derive(secret)
cypher = enc_config['cypher'].encode('utf-8')
config = json.loads(jwe.decrypt(cypher, key))
return config
def main():
secret = b"Passphrase132,.<>/'\""
with open('test-config.json') as f:
config = json.load(f)
pprint(config, indent=2)
enc_config = encrypt_config(config, secret)
pprint(enc_config, indent=2)
with open('enc-config.json', 'w') as f:
json.dump(enc_config, f)
dec_config = decrypt_config(enc_config, secret)
pprint(dec_config, indent=2)
with open('dec-config.json', 'w') as f:
json.dump(dec_config, f)
print(json.dumps(config) == json.dumps(dec_config))
if __name__ == "__main__":
main()