Create a gist now

Instantly share code, notes, and snippets.

@4poc /1_encrypt.py
Last active Dec 13, 2016

Embed
Is this secure? (encrypted and signed(?))
import jose
from json import dumps
from Crypto.PublicKey import RSA
key = RSA.generate(2048)
private_key = key.exportKey('PEM').decode('utf-8')
public_key = key.publickey().exportKey('PEM').decode('utf-8')
from jwcrypto import jwk, jwe
from jwcrypto.common import json_encode
payload = {
'url': 'https://jwcrypto.readthedocs.io/en/stable/jwe.html#examples',
'user': 'asdfasdfasdf asdf saa sd fdas'
}
jwk_public_key = jwk.JWK.from_pem(bytes(public_key, 'utf-8'))
jwetoken = jwe.JWE(dumps(payload), json_encode({'alg': 'RSA-OAEP', 'enc': 'A128CBC-HS256'}))
jwetoken.add_recipient(jwk_public_key)
encrypted = jwetoken.serialize(True) # True for compact
var jose = require('node-jose');
const keystore = jose.JWK.createKeyStore();
const private_key = ...;
keystore.add(private_key, 'pem').
then((result) => {
console.log(result);
jose.JWE.createDecrypt(keystore).
decrypt(payload).
then((result) => {
console.log(result.plaintext.toString());
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment