gpt4 book ai didi

java - Phpseclib 等同于 Java RSA 加密

转载 作者:行者123 更新时间:2023-11-30 08:06:42 40 4
gpt4 key购买 nike

我正在尝试使用 Cipher cipher = Cipher.getInstance("RSA"); 以及 PHP 和 phpseclib 来重现 Java 加密。

我试过这个和很多东西,但数据似乎没有正确加密

$rsa = new Crypt_RSA();
$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_OAEP);
$rsa->loadKey($pub_key);
$ciphertext = $rsa->encrypt($plaintext);

我尝试了不同的组合,比如

$rsa->setMGFHash('sha512');
$rsa->setHash('sha512');
//$rsa->setMGFHash('sha256');
//$rsa->setHash('sha256');

没有成功。

我错过了什么吗?

最佳答案

永远不要使用像这样的不完整的密码字符串:

Cipher cipher = Cipher.getInstance("RSA");

这没有指定填充,因此取决于默认安全提供程序喜欢的填充。这可能默认为:

Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");

这将兼容

$rsa = new Crypt_RSA();
$rsa->loadKey($pub_key);
$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
$ciphertext = $rsa->encrypt($plaintext);

但是您不应该再使用 PKCS#1 v1.5 填充。你真的应该使用 OAEP ( meaning ):

Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");

和 phpseclib 等价物应该是

$rsa = new Crypt_RSA();
$rsa->loadKey($pub_ley);
$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_OAEP);
$rsa->setHash('sha256');
$ciphertext = $rsa->encrypt($plaintext);

关于java - Phpseclib 等同于 Java RSA 加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34187124/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com