我按照这个
尝试了JWE加密和解密Using node-jose, how do I decrypt the data I just encrypted?
var ursa = require("ursa");
const {
JWK
} = require('node-jose');
const keygen = require('generate-rsa-keypair');
const jose = require('./joseHelper');
const rawKeys = keygen();
const makeKey = pem => JWK.asKey(pem, 'pem');
async function start() {
const publicKey = await makeKey(rawKeys.public);
const privateKey = await makeKey(rawKeys.private);
const raw = {
iss: 'test',
exp: new Date().getTime() + 3600,
sub: {
test: 'This is a test',
},
};
const {
encrypt,
decrypt
} = jose(publicKey, privateKey);
return encrypt(raw).then(encrypted => {
console.log(encrypted);
decrypt(encrypted);
});
}
return start().then((result) => {
console.log('decrypted', result);
}, (err) => {
console.error(err);
});
JoseHelper
const {
JWE
} = require('node-jose');
const jose = (publicKey, privateKey) => {
async function encrypt(raw) {
if (!raw) throw new Error('Missing raw data.');
const buffer = new Buffer(JSON.stringify(raw));
return JWE.createEncrypt(publicKey).update(buffer).final();
}
async function decrypt(encrypted) {
if (!encrypted) throw new Error('Missing encrypted data.');
return JWE.createDecrypt(privateKey).decrypt(encrypted);
}
return {
encrypt,
decrypt
}
}
module.exports = jose;
运行此程序时,我将解密为null。我错过了什么?