encryption - OAEPwithMD5andMGF1Padding in node-rsa

标签 encryption rsa

我想弄清楚如何在 node-rsa 中使用 OAEPwithMD5andMGF1Padding 进行 RSA 加密。

下面是我在 node.js 中的代码。

    var NodeRSA = require('node-rsa');
    var fs = require('fs');
    var publicKey = '-----BEGIN PUBLIC KEY-----\n*****\n-----END PUBLIC KEY-----';
    var privateKey = '-----BEGIN RSA PRIVATE KEY-----\n*****\n-----END RSA PRIVATE KEY-----'
    const constants = require('constants');
    var options1 = {
      environment: 'node',
      encryptionScheme: {
        scheme: 'pkcs1_oaep',
        hash: 'md5', //hash using for scheme
      }
    }
    var text = 'This is the string to be encrypted using RSA!';
    var encryptKey = new NodeRSA(publicKey, 'pkcs8-public', options1);
    encryptKey.setOptions(options1)

    var encrypted = encryptKey.encrypt(text, 'base64');
    console.log(encrypted);
    console.log(encryptKey.isPublic(true))

    var options2  = {
      environment: 'node',
      encryptionScheme: {
        scheme: 'pkcs1_oaep', //scheme
        hash: 'md5', //hash using for scheme
      }
    }

var decryptKey = new NodeRSA(privateKey, 'pkcs1', options2);
decryptKey.setOptions(options2)
var decrypted = decryptKey.decrypt(encrypted, 'utf8');
console.log('decrypted: ', decrypted);

运行上述代码的结果。

f1zi49yKJSqkWW2J3Jt2lf1fe79JgqufFawYESOJRqhM4YEcGQBcaP39yptn7vShhsJBCTUOsbiV1YcW/YUzoaSQzX9YU0iTMara7h+LNLUrq4FZ2twy5X3uyAP1sUD1SnvQvlRJqrAh23UAwnx31rv6ySC+XgpLPR7wHYaDbSgyQKiF3qhGRj2SIAZ6weziNPfEm9FifBVjnWMvGDQYbjLbanbnSriN+bWpRtXKH9pQqMoskkiMwCviJdKtKzz/vVr0littPLnw0ojbsGSPKQPS3U3xCH3QiBmxEegc0uy3sJdk6aH/2SMuoPzGu7VS+PsLQctxnvKNnC9qsLFWyA==
true
decrypted:  This is the string to be encrypted using RSA!

下面是我的JAVA代码

 import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.security.KeyFactory;
import java.security.interfaces.RSAPrivateKey;
import java.security.spec.KeySpec;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;

import javax.crypto.Cipher;


public class DecryptATT {
    public static void main(String[] args) throws Exception {
        String encryptedData = "f1zi49yKJSqkWW2J3Jt2lf1fe79JgqufFawYESOJRqhM4YEcGQBcaP39yptn7vShhsJBCTUOsbiV1YcW/YUzoaSQzX9YU0iTMara7h+LNLUrq4FZ2twy5X3uyAP1sUD1SnvQvlRJqrAh23UAwnx31rv6ySC+XgpLPR7wHYaDbSgyQKiF3qhGRj2SIAZ6weziNPfEm9FifBVjnWMvGDQYbjLbanbnSriN+bWpRtXKH9pQqMoskkiMwCviJdKtKzz/vVr0littPLnw0ojbsGSPKQPS3U3xCH3QiBmxEegc0uy3sJdk6aH/2SMuoPzGu7VS+PsLQctxnvKNnC9qsLFWyA==";
//      Cipher decrypt = Cipher.getInstance("RSA/ECB/OAEPwithMD5andMGF1Padding");

        Cipher decrypt = Cipher.getInstance("RSA/ECB/OAEPwithSHA1andMGF1Padding");

        RSAPrivateKey privateKey = getPrivateKey();

        System.out.println("test");

        decrypt.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] original = decrypt.doFinal(Base64.getDecoder().decode(encryptedData));

        System.out.println(new String(original));

    }

    public static RSAPrivateKey getPrivateKey() throws Exception {
        String keyPath = "/Users/C.SubbiahVeluAngamuthu/Desktop/Samsung/Docs/att/Keys/3_my_testing/pkcs8_key";
        File privKeyFile = new File(keyPath);
        BufferedInputStream bis = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(privKeyFile));
        } catch (FileNotFoundException e) {
            throw new Exception("Could not locate keyfile at '" + keyPath + "'", e);
        }
        byte[] privKeyBytes = new byte[(int) privKeyFile.length()];
        bis.read(privKeyBytes);
        bis.close();
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        KeySpec ks = new PKCS8EncodedKeySpec(privKeyBytes);
        RSAPrivateKey privKey = (RSAPrivateKey) keyFactory.generatePrivate(ks);
        return privKey;
    }


}

JAVA代码运行结果如下

test
This is the string to be encrypted using RSA!

但是当我将密码实例从 RSA/ECB/OAEPwithSHA1andMGF1Padding 更改为 "RSA/ECB/OAEPwithMD5andMGF1Padding"(我假设是我在node.js 程序的加密方案)它抛出以下错误

test
Exception in thread "main" javax.crypto.BadPaddingException: Decryption error
    at sun.security.rsa.RSAPadding.unpadOAEP(RSAPadding.java:499)
    at sun.security.rsa.RSAPadding.unpad(RSAPadding.java:293)
    at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:363)
    at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:389)
    at javax.crypto.Cipher.doFinal(Cipher.java:2165)
    at DecryptATT.main(DecryptATT.java:26)

有人能帮我看看哪里出了问题吗?

最佳答案

RSAES-OAEP

参数化
  • OAEP 使用的散列 Hash,其宽度以八位字节表示 hLen
  • 公钥的大小,k 个八位字节
  • OAEP 使用的掩码生成函数 (MGF)

MGF 几乎总是 MFG1 ,它本身由 MFG1 使用的哈希 Hash' 参数化,它的八位字节宽度标记为 hLen'(' 不在标准,我正在编造这个符号)。

你猜对了,有注释说明 HashHash' 是相同的,甚至 hLen= hLen'

而且,相信我,除非对此做一些特别的事情,否则在典型的 Java 环境下 "RSA/ECB/OAEPwithMD5andMGF1Padding"(如果支持)将使用 MD5 作为 Hash 但默认为 Hash' 的 SHA-1;当 node.js 可能对两者都使用 MD5 时。

面对 SHA-256 而不是 MD5 的类似问题,我们可以强制 Java 运行时做正确的事情

Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
cipher.init(Cipher.DECRYPT_MODE, privKey, new OAEPParameterSpec(
    "SHA-256", "MGF1", MGF1ParameterSpec.SHA256, PSource.PSpecified.DEFAULT
));

我担心你不会那么幸运,因为MGF1ParameterSpec似乎从来没有 MD5 说明符;但也许在放弃之前尝试 new MGF1ParameterSpec("MD5") 获得一个。

如果确实需要在 Java 下完成工作,一种选择是在使用“RSA/ECB/NoPadding”调用的 Cipher 之上滚动带有 MD5 的 RSAES-OAEP,这将执行教科书 RSA,迄今为止最复杂的构建 block (至少,所有 key 管理、模块化算法和卸载到 HSM 的能力都得到了处理)。这是几十行代码,包括 MFG1。

另一个选项可能是 BouncyCastle .


继续使用 MD5 是个坏主意,即使在 MFG1 中也是如此。如果对手可以在知道该部分之前的内容时选择消息的一部分,那么将其用作主要哈希将是一场彻头彻尾的灾难。如有疑问,请不要使用 MD5

关于encryption - OAEPwithMD5andMGF1Padding in node-rsa,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49583427/

相关文章:

mysql - mysql有非对称加密功能吗?

java - 用 C# 解密用 Java 加密的密码

node.js - 使用 SHA256 签名与使用 RSA-SHA256 签名之间的区别

从 .pem 文件读取时的 python "RSA key format is not supported"

ssl - 如何使用 'TLS1_ECDHE_RSA' 密码为 fips 平台生成带有 Openssl 的 pem 证书

c++ - 加密/解密期间 Crypto++ 显式销毁?

android - 在 iOS 上解密 android AES 示例

java - 在 iOS/Swift 中创建并导出为 base64 的 RSA 公钥在 Java 中无法识别

c# - ZMQ消息传递特定端点

c - 使用 C 中的 Miracle Library 进行 ECC 加密