简体   繁体   English

Nodejs crypto.publicEncrypt 在不同的机器上产生不同的结果

[英]Nodejs crypto.publicEncrypt yields different results on different machines

We are trying to encrypt and decrypt using an asymmetric key in AWS KMS.我们正在尝试使用 AWS KMS 中的非对称密钥进行加密和解密。 The configuration for the key is as follows:密钥的配置如下:

AWS 非对称密钥配置

In NodeJS, we use the public key to encrypt via the crypto.publicEncrypt:在 NodeJS 中,我们使用公钥通过 crypto.publicEncrypt 进行加密:

const encryptRSAPayload = (buffer, publicKey) => {
  const encryptedBuffer = crypto.publicEncrypt(
    {
      key: publicKey,
      oaepHash: 'sha256',
      padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
    },
    buffer
  );

  return encryptedBuffer;
};

And we use the function like this (the public key is read from a local file during the minimal repro):我们像这样使用 function (在最小复制期间从本地文件中读取公钥):

  const plainText = '12345678910';
  const encrypted = await encryptRSAPayload(Buffer.from(plainText), publicKey);

Now, four developers have ran the exact same code (zipped, with public key etc), this is happening:现在,四个开发人员运行了完全相同的代码(压缩、使用公钥等),这正在发生:

NOTE: All of the developers are on the latest OSX system.注意:所有开发人员都在最新的 OSX 系统上。

  1. Two of us can use AWS to decrypt whatever we produce from the encrypt function, and the other two can not (failing with IvalidCiphertext: null) from AWS.我们中的两个人可以使用 AWS 来解密我们从加密 function 中产生的任何内容,而另外两个人则不能(由于 IvalidCiphertext: null 失败)来自 AWS。

  2. The encrypted, base64 string from one of the machines that can not encrypt -> decrypt, can not be decrypted on any other machine.来自无法加密->解密的机器之一的加密 base64 字符串,无法在任何其他机器上解密。

  3. The encrypted base64 string from one of the machines that can encrypt -> decrypt, can be decrypted in aws from any machine.来自可以加密->解密的机器之一的加密 base64 字符串可以在任何机器的aws中解密。

By now, ive spent two days on this and am a bit lost on what to do.到目前为止,我在这上面花了两天时间,有点不知所措。 Any ideas?有任何想法吗?

Problem solved after a few more days of debugging.经过几天的调试,问题解决了。 The problem stemmed from the shipped version of OpenSSL that comes with OSX.问题源于 OSX 随附的 OpenSSL 的出厂版本。 For me, that was LibreSSL 2.8, which does not include some of the padding flags used in OAEP nor changing the hash to sha256 (instead of sha1).对我来说,那是 LibreSSL 2.8,它不包括 OAEP 中使用的一些填充标志,也不将 hash 更改为 sha256(而不是 sha1)。

The solution was:解决方案是:

  1. Install OpenSSL via Homebrew and set the PATH env to use that version instead of the shipped version.通过 Homebrew 安装 OpenSSL 并将 PATH 环境设置为使用该版本而不是出厂版本。
  2. Reinstall any installed node version to re-link to the correct OpenSSL version.重新安装任何已安装的节点版本以重新链接到正确的 OpenSSL 版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM