Java 的 RSA/ECB/OAEPWithSHA-256AndMGF1Padding 相当于 Node.js

Java's RSA/ECB/OAEPWithSHA-256AndMGF1Padding equivalent in Node.js

数据解密将在JAVA中运行使用RSA/ECB/OAEPWithSHA-256AndMGF1Padding算法。所以我必须使用等同于 node.js 中的 RSA/ECB/OAEPWithSHA-256AndMGF1Padding 的算法用 public 密钥加密数据。

我尝试使用 crypto.publicEncrypt(key, buffer),它使用 crypto.constants.RSA_PKCS1_OAEP_PADDING,这与上述算法不相似。 所以我需要等同于 "RSA/ECB/OAEPWithSHA-256AndMGF1Padding" 的算法或者如何在 node.js

中实现相同的算法

首先,你不应该使用"ECB"模式密码,因为:

  1. ECB 是分组密码模式,RSA 不是基于该操作模式的算法。
  2. 如果您使用基于该操作模式的算法(例如 AES),则不应使用 ECB,因为它没有 IV(初始化向量),因此它不安全并且加密分析器可能会损坏密码。如果您想与外部系统共享敏感信息并防止 Oracle Padding,您可以使用 CBC,它有 IV 或 GCM。我推荐你访问以下link:

MSC61-J. Do not use insecure or weak cryptographic algorithms

因此,在这种情况下,您只需要使用 OAEP 进行 RSA 加密,因为它是一种填充方案,它有助于防止 Oracle Padding 用于非对称算法,然后将您的代码更改为:RSA/None/OAEPWithSHA-256AndMGF1Padding。也许,您可以获得与 Node.js 的兼容性。另外,我建议您访问官方网站:

JCA Reference Guide

希望这些信息对您有所帮助。

祝你好运。

我终于找到了答案。 相当于 "RSA/ECB/OAEPWithSHA-256AndMGF1Padding" 可以通过 node-forge npm 模块实现。 https://www.npmjs.com/package/node-forge#rsa

    // encrypt data with a public key using RSAES-OAEP/SHA-256/MGF1-SHA-1
// compatible with Java's RSA/ECB/OAEPWithSHA-256AndMGF1Padding
var encrypted = publicKey.encrypt(bytes, 'RSA-OAEP', {
  md: forge.md.sha256.create(),
  mgf1: {
    md: forge.md.sha256.create()
  }
});

谢谢