使用node-jose,如何解密刚刚加密的数据? [英] Using node-jose, how do I decrypt the data I just encrypted?

查看:433
本文介绍了使用node-jose,如何解密刚刚加密的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 node-jose .

I am trying to implement simple JOSE encrypt and decrypt functions using node-jose.

我的代码如下(使用Node 8.2.1编写)

My code is as follows (written using Node 8.2.1)

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.')
    const buffer = new Buffer(JSON.stringify(encrypted));
    return JWE.createDecrypt(privateKey).decrypt(buffer);
  }

  return { encrypt, decrypt }
}

module.exports = jose;

我使用 generate-rsa-keypair 生成RSA密钥对.

I generate an RSA keypair using generate-rsa-keypair.

因此通过此代码测试事物的加密方面可以正常工作

So testing via this code the encryption side of things works fine

const { JWK } = require('node-jose');
const keygen = require('generate-rsa-keypair');
const jose = require('./src/utils/jose');

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 => decrypt(encrypted));
}

return start().then((result) => {
  console.log('decrypted', result)
}, (err) => {
  console.error(err);
});

encrypted结果是

{
  recipients: [ { encrypted_key: 'ciNiK6Unq30zCAXxIl2Dx9b8bZAi79qbpL1yUCwTFnSghFLrIZ11_D2ozt5on3r3ThUu96oDLZPcNShbqWPMV49NvQAsSNGdemhgzmTt3Lf3rJn1YiqvJvqf5NIXdmzjdoEZi-d9224mGpZGVKtIIFeT6-0hYgm5zNqq_aF_X2jy5IiF-mAGspNdXIk_KXPrTVbnU-XL9J5aAoG2Lp51Te1WzGA4Fjg4Ve5ZTzH6TLlQ5R5Ob_14liK-INrSi3armwXrtMgJcTmI_4oBtORtZp8AjaXzecFO_GzifvRVCSKx2vmpy9KaECpskMhZBHVx9RX9cvGKh7hq3Y7vsUucZw' } ],
  protected: 'eyJhbGciOiJSU0EtT0FFUCIsImtpZCI6IldLWS1ONDRXM2RnanA4U2ZxSlp3TldqV3AzUG1XZ29UczhjRDh3eWNSUWciLCJlbmMiOiJBMTI4Q0JDLUhTMjU2In0',
  iv: 'wvqir2ewtQPfDHQtzl6IUg',
  ciphertext: 'ZwIrL_3739LI17rh3gWDUA6lXIL7ewkSh54FO_RwumC0qh9B0DcAr8RyXsfPbW19cV4u7SbZNSRP6B8qNOTy-2iENlqBISfE_kolDt8g5sg',
  tag: 'z8nwrJfRgOi1hYMBI9lGeQ'
}

但是当我尝试解密时,我会得到

but when I try to decrypt that I get

Error: no key found
  at processKey (node_modules/node-jose/lib/jwe/decrypt.js:157:22)

使用node-jose的示例很少,所以我不确定以下内容

There are very few examples of using node-jose so I am unsure of the following

  1. 我假设我应该使用私钥进行解密.但这只是一个假设.没有一个示例显示使用公钥/私钥对,只是一个密钥.
  2. 我假设加密的结果可以被简单化并变成一个缓冲区,然后传递给decrypt,但事实并非如此.
  1. I am assuming I ought to be decrypting with the private key. But that's just an assumption. None of the examples show use of public/private key pairs, just a single key.
  2. I'm assuming that the results of the encryption can just be strringified and turned into a buffer and passed into decrypt but perhaps that's not the case.

这真的如何工作?

推荐答案

  1. 使用公钥/私钥对时,private密钥用于解密,public密钥用于加密.

  1. When using public/private key pairs, the private key is used to decrypt and the public key is used to encrypt.

JWEDecrypter.decrypt()的输入是来自JWEEncrypter.final()的承诺输出.

The input to JWEDecrypter.decrypt() is the promised output from JWEEncrypter.final().

将您的decrypt函数更改为:

async function decrypt(encrypted) {
  if (!encrypted) throw new Error('Missing encrypted data.')
  return JWE.createDecrypt(privateKey).decrypt(encrypted);
}

这篇关于使用node-jose,如何解密刚刚加密的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆