[英]Phpseclib equivalent to Java RSA Encryption
我正在嘗試使用Cipher cipher = Cipher.getInstance("RSA");
重現Java加密Cipher cipher = Cipher.getInstance("RSA");
與PHP和phpseclib。
我嘗試了很多,但是似乎數據沒有正確加密
$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( 含義 ):
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.