简体   繁体   English

Java的RSA / ECB / OAEPWITHSHA-256ANDMGF1PADDING在PHP中等效

[英]Java's RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING equivalent in PHP

I have a case where a 'secret' is coming to me from a Java App and it's cipher'd using a public key and the RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING cipher. 我有一个案例,其中一个“秘密”从Java应用程序中传来,并且使用公开密钥和RSA / ECB / OAEPWITHSHA-256ANDMGF1PADDING密码进行加密。 I'm trying to decipher it at my end, but I'm not sure how to get the equivalent of that cipher. 我试图在最后解密它,但是我不确定如何获得与该密码相同的结果。 I've been using phpseclib for other security stuff, and I've tried the OAEP encryption mode in there, but to no avail. 我一直在将phpseclib用于其他安全性方面,并且在那里尝试过OAEP加密模式,但无济于事。 I just get a decrypt error with no information. 我只是收到一个没有信息的解密错误。 I just want to state that the keys are correct: 我只想声明密钥是正确的:

function oaes_decrypt($ciphertext, $privatekey) {
    $rsa = new \Crypt_RSA();
    $rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_OAEP);
    $rsa->setMGFHash('sha256');
    $rsa->setHash('sha256');
    $rsa->loadKey($privatekey);

    return $rsa->decrypt($ciphertext);
}

Any help will be greatly appreciated! 任何帮助将不胜感激! Thank you! 谢谢!

Try $rsa->setMGFHash('sha1'); 尝试$rsa->setMGFHash('sha1'); The SHA-256 in RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING doesn't refer to the MGF1 hash. RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING中的SHA-256不引用MGF1哈希。 To have that be sha256 you'd have to be doing this: 要使它成为sha256,您必须这样做:

Cipher oaepFromInit = Cipher.getInstance("RSA/ECB/OAEPPadding");
OAEPParameterSpec oaepParams = new OAEPParameterSpec("SHA-256", "MGF1", new MGF1ParameterSpec("SHA-1"), PSpecified.DEFAULT);
oaepFromInit.init(Cipher.DECRYPT_MODE, privkey, oaepParams);
byte[] pt = oaepFromInit.doFinal(ct);
System.out.println(new String(pt, StandardCharsets.UTF_8));

The final result, that works, in PHP: 最终结果在PHP中有效:

function oaes_decrypt($ciphertext, $privatekey) {
    $rsa = new \Crypt_RSA();
    $rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_OAEP);
    $rsa->setMGFHash('sha1');
    $rsa->setHash('sha256');
    $rsa->loadKey($privatekey);

    return $rsa->decrypt($ciphertext);
}

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

相关问题 Java 的 RSA/ECB/OAEPWithSHA-256AndMGF1Padding 在 Node.js 中的等价物 - Java's RSA/ECB/OAEPWithSHA-256AndMGF1Padding equivalent in Node.js C#等效于Java RSA / ECB / OAEPWithSHA-256AndMGF1Padding - C# equivalent to Java RSA/ECB/OAEPWithSHA-256AndMGF1Padding 获取 ruby 的“RSA/ECB/OAEPWithSHA-256AndMGF1Padding”组合 - Get this “RSA/ECB/OAEPWithSHA-256AndMGF1Padding” combination for ruby java RSA/ECB/OAEPWithSHA 256AndMGF1Padding 在 golang 中等效 - java RSA/ECB/OAEPWithSHA 256AndMGF1Padding equivalent in golang 分解 RSA/ECB/OAEPWithSHA-256AndMGF1Padding - Breaking down RSA/ECB/OAEPWithSHA-256AndMGF1Padding “RSA/ECB/OAEPWITHSHA256ANDMGF1PADDING”和“RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING”之间的区别是什么 - what is deference between "RSA/ECB/OAEPWITHSHA256ANDMGF1PADDING" and "RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING" C# Bouncy Castle 中的 RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING - 对于 RSA 密码输入太大 - RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING in C# Bouncy Castle - input too large for RSA cipher RSA / ECB / OAEPWithSHA-256AndMGF1Padding但MGF1使用SHA-256? - RSA/ECB/OAEPWithSHA-256AndMGF1Padding but with MGF1 using SHA-256? 错误的填充例外-pkcs11中的RSA / ECB / OAEPWITHSHA-256ANDMGF1PADDING - Bad Padding Exception - RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING in pkcs11 找不到任何支持 RSA/None/OAEPWITHSHA-256ANDMGF1PADDING 的提供程序 - Cannot find any provider supporting RSA/None/OAEPWITHSHA-256ANDMGF1PADDING
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM