如何在 PHP 上使用 openssl 使用 SHA256 解密 RSA OAEP
How to Decrypt RSA OAEP with SHA256 using openssl on PHP
我尝试在 php 上使用 opensll 解密。所需的算法是带有 sha256 的 rsa-oaep。我需要为 $cipher_algo 写什么?
$key = 'examplekey';
$secret = 'examplesecret';
$cipher_algo = '';
$out = openssl_decrypt(base64_decode($secret), $cipher_algo, $key, OPENSSL_RAW_DATA);
或一些可以在php
上使用的加密和解密库
您需要库 https://phpseclib.com/ 才能将以下代码获取到 运行(我使用的是版本 3)。
请注意,代码中没有异常处理。
这是一个输出:
RSA 2048 encryption OAEP SHA-256 string
plaintext: The quick brown fox jumps over the lazy dog
* * * encrypt the plaintext with the RSA public key * * *
ciphertextBase64: p1SRx16ieLkDOu0SzFblU0WYSVNmspyJW57KG/fXoqvdqQGtO3qKXxPKIe24XBP4I+APx4pdvt3Ye/qVDMxokSzbLLPKplPs6P9NKoyJnrliIK8qUeP921d9G+x2XcRTmQgyCEaukp8sJuqC4gfaBe+houD8AG3jA4h2fBaBepZa9ZYwsMp4UWifCxvb5oWFst5rG3yMNNlg4hH+pFuv3iBT8qYqyznKGFrFRcrQ/KFGrYUtQ62+ak6NKAC0MJCl7AsERFtSW3Zfd/X1hRJun4Wgu/IEjeVw0nX5U78zo4eim7sXkGySFp8YLaH8+bu3rkGI0QT6u3R1NBc20lWjEw==
* * * decrypt the ciphertext with the RSA private key * * *
ciphertextReceivedBase64: p1SRx16ieLkDOu0SzFblU0WYSVNmspyJW57KG/fXoqvdqQGtO3qKXxPKIe24XBP4I+APx4pdvt3Ye/qVDMxokSzbLLPKplPs6P9NKoyJnrliIK8qUeP921d9G+x2XcRTmQgyCEaukp8sJuqC4gfaBe+houD8AG3jA4h2fBaBepZa9ZYwsMp4UWifCxvb5oWFst5rG3yMNNlg4hH+pFuv3iBT8qYqyznKGFrFRcrQ/KFGrYUtQ62+ak6NKAC0MJCl7AsERFtSW3Zfd/X1hRJun4Wgu/IEjeVw0nX5U78zo4eim7sXkGySFp8YLaH8+bu3rkGI0QT6u3R1NBc20lWjEw==
decryptedtext: The quick brown fox jumps over the lazy dog
代码:
<?php
include 'vendor/autoload.php';
// uses phpseclib version 3
use phpseclib3\Crypt\PublicKeyLoader;
function rsaEncryptionOaepSha256($publicKey, $plaintext) {
$rsa = PublicKeyLoader::load($publicKey)
->withHash('sha256')
->withMGFHash('sha256');
return $rsa->encrypt($plaintext);
}
function rsaDecryptionOaepSha256($privateKey, $ciphertext) {
$rsa = PublicKeyLoader::load($privateKey)
->withHash('sha256')
->withMGFHash('sha256');
return $rsa->decrypt($ciphertext);
}
function base64Encoding($input) {return base64_encode($input);}
function base64Decoding($input){return base64_decode($input);}
function loadRsaPrivateKeyPem() {
// this is a sample key - don't worry !
return '
-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDwSZYlRn86zPi9
e1RTZL7QzgE/36zjbeCMyOhf6o/WIKeVxFwVbG2FAY3YJZIxnBH+9j1XS6f+ewjG
FlJY4f2IrOpS1kPiO3fmOo5N4nc8JKvjwmKtUM0t63uFFPfs69+7mKJ4w3tk2mSN
4gb8J9P9BCXtH6Q78SdOYvdCMspA1X8eERsdLb/jjHs8+gepKqQ6+XwZbSq0vf2B
MtaAB7zTX/Dk+ZxDfwIobShPaB0mYmojE2YAQeRq1gYdwwO1dEGk6E5J2toWPpKY
/IcSYsGKyFqrsmbw0880r1BwRDer4RFrkzp4zvY+kX3eDanlyMqDLPN+ghXT1lv8
snZpbaBDAgMBAAECggEBAIVxmHzjBc11/73bPB2EGaSEg5UhdzZm0wncmZCLB453
XBqEjk8nhDsVfdzIIMSEVEowHijYz1c4pMq9osXR26eHwCp47AI73H5zjowadPVl
uEAot/xgn1IdMN/boURmSj44qiI/DcwYrTdOi2qGA+jD4PwrUl4nsxiJRZ/x7PjL
hMzRbvDxQ4/Q4ThYXwoEGiIBBK/iB3Z5eR7lFa8E5yAaxM2QP9PENBr/OqkGXLWV
qA/Y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IC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA8EmWJUZ/Osz4vXtUU2S+
0M4BP9+s423gjMjoX+qP1iCnlcRcFWxthQGN2CWSMZwR/vY9V0un/nsIxhZSWOH9
iKzqUtZD4jt35jqOTeJ3PCSr48JirVDNLet7hRT37Ovfu5iieMN7ZNpkjeIG/CfT
/QQl7R+kO/EnTmL3QjLKQNV/HhEbHS2/44x7PPoHqSqkOvl8GW0qtL39gTLWgAe8
01/w5PmcQ38CKG0oT2gdJmJqIxNmAEHkatYGHcMDtXRBpOhOSdraFj6SmPyHEmLB
ishaq7Jm8NPPNK9QcEQ3q+ERa5M6eM72PpF93g2p5cjKgyzzfoIV09Zb/LJ2aW2g
QwIDAQAB
-----END PUBLIC KEY-----
';
}
echo 'RSA 2048 encryption OAEP SHA-256 string' . PHP_EOL;
$dataToEncryptString = "The quick brown fox jumps over the lazy dog";
echo 'plaintext: ' . $dataToEncryptString . PHP_EOL;
// # # # usually we would load the private and public key from a file or keystore # # #
// # # # here we use hardcoded keys for demonstration - don't do this in real programs # # #
// encryption
echo PHP_EOL . '* * * encrypt the plaintext with the RSA public key * * *' .PHP_EOL;
$ciphertextBase64 = base64Encoding(rsaEncryptionOaepSha256(loadRsaPublicKeyPem(), $dataToEncryptString));
echo 'ciphertextBase64: ' . $ciphertextBase64 . PHP_EOL;
// transport the encrypted data to recipient
// receiving the encrypted data, decryption
echo PHP_EOL . '* * * decrypt the ciphertext with the RSA private key * * *' .PHP_EOL;
$ciphertextReceivedBase64 = $ciphertextBase64;
echo 'ciphertextReceivedBase64: ' . $ciphertextReceivedBase64 . PHP_EOL;
$decryptedtext = rsaDecryptionOaepSha256(loadRsaPrivateKeyPem(), base64Decoding($ciphertextReceivedBase64));
echo 'decryptedtext: ' . $decryptedtext . PHP_EOL;
?>
我尝试在 php 上使用 opensll 解密。所需的算法是带有 sha256 的 rsa-oaep。我需要为 $cipher_algo 写什么?
$key = 'examplekey';
$secret = 'examplesecret';
$cipher_algo = '';
$out = openssl_decrypt(base64_decode($secret), $cipher_algo, $key, OPENSSL_RAW_DATA);
或一些可以在php
上使用的加密和解密库您需要库 https://phpseclib.com/ 才能将以下代码获取到 运行(我使用的是版本 3)。
请注意,代码中没有异常处理。
这是一个输出:
RSA 2048 encryption OAEP SHA-256 string
plaintext: The quick brown fox jumps over the lazy dog
* * * encrypt the plaintext with the RSA public key * * *
ciphertextBase64: p1SRx16ieLkDOu0SzFblU0WYSVNmspyJW57KG/fXoqvdqQGtO3qKXxPKIe24XBP4I+APx4pdvt3Ye/qVDMxokSzbLLPKplPs6P9NKoyJnrliIK8qUeP921d9G+x2XcRTmQgyCEaukp8sJuqC4gfaBe+houD8AG3jA4h2fBaBepZa9ZYwsMp4UWifCxvb5oWFst5rG3yMNNlg4hH+pFuv3iBT8qYqyznKGFrFRcrQ/KFGrYUtQ62+ak6NKAC0MJCl7AsERFtSW3Zfd/X1hRJun4Wgu/IEjeVw0nX5U78zo4eim7sXkGySFp8YLaH8+bu3rkGI0QT6u3R1NBc20lWjEw==
* * * decrypt the ciphertext with the RSA private key * * *
ciphertextReceivedBase64: p1SRx16ieLkDOu0SzFblU0WYSVNmspyJW57KG/fXoqvdqQGtO3qKXxPKIe24XBP4I+APx4pdvt3Ye/qVDMxokSzbLLPKplPs6P9NKoyJnrliIK8qUeP921d9G+x2XcRTmQgyCEaukp8sJuqC4gfaBe+houD8AG3jA4h2fBaBepZa9ZYwsMp4UWifCxvb5oWFst5rG3yMNNlg4hH+pFuv3iBT8qYqyznKGFrFRcrQ/KFGrYUtQ62+ak6NKAC0MJCl7AsERFtSW3Zfd/X1hRJun4Wgu/IEjeVw0nX5U78zo4eim7sXkGySFp8YLaH8+bu3rkGI0QT6u3R1NBc20lWjEw==
decryptedtext: The quick brown fox jumps over the lazy dog
代码:
<?php
include 'vendor/autoload.php';
// uses phpseclib version 3
use phpseclib3\Crypt\PublicKeyLoader;
function rsaEncryptionOaepSha256($publicKey, $plaintext) {
$rsa = PublicKeyLoader::load($publicKey)
->withHash('sha256')
->withMGFHash('sha256');
return $rsa->encrypt($plaintext);
}
function rsaDecryptionOaepSha256($privateKey, $ciphertext) {
$rsa = PublicKeyLoader::load($privateKey)
->withHash('sha256')
->withMGFHash('sha256');
return $rsa->decrypt($ciphertext);
}
function base64Encoding($input) {return base64_encode($input);}
function base64Decoding($input){return base64_decode($input);}
function loadRsaPrivateKeyPem() {
// this is a sample key - don't worry !
return '
-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDwSZYlRn86zPi9
e1RTZL7QzgE/36zjbeCMyOhf6o/WIKeVxFwVbG2FAY3YJZIxnBH+9j1XS6f+ewjG
FlJY4f2IrOpS1kPiO3fmOo5N4nc8JKvjwmKtUM0t63uFFPfs69+7mKJ4w3tk2mSN
4gb8J9P9BCXtH6Q78SdOYvdCMspA1X8eERsdLb/jjHs8+gepKqQ6+XwZbSq0vf2B
MtaAB7zTX/Dk+ZxDfwIobShPaB0mYmojE2YAQeRq1gYdwwO1dEGk6E5J2toWPpKY
/IcSYsGKyFqrsmbw0880r1BwRDer4RFrkzp4zvY+kX3eDanlyMqDLPN+ghXT1lv8
snZpbaBDAgMBAAECggEBAIVxmHzjBc11/73bPB2EGaSEg5UhdzZm0wncmZCLB453
XBqEjk8nhDsVfdzIIMSEVEowHijYz1c4pMq9osXR26eHwCp47AI73H5zjowadPVl
uEAot/xgn1IdMN/boURmSj44qiI/DcwYrTdOi2qGA+jD4PwrUl4nsxiJRZ/x7PjL
hMzRbvDxQ4/Q4ThYXwoEGiIBBK/iB3Z5eR7lFa8E5yAaxM2QP9PENBr/OqkGXLWV
qA/YTxs3gAvkUjMhlScOi7PMwRX9HsrAeLKbLuC1KJv1p2THUtZbOHqrAF/uwHaj
ygUblFaa/BTckTN7PKSVIhp7OihbD04bSRrh+nOilcECgYEA/8atV5DmNxFrxF1P
ODDjdJPNb9pzNrDF03TiFB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qUtZD4jt35jqOTeJ3PCSr48JirVDNLet7hRT37Ovfu5iieMN7ZNpkjeIG/CfT
/QQl7R+kO/EnTmL3QjLKQNV/HhEbHS2/44x7PPoHqSqkOvl8GW0qtL39gTLWgAe8
01/w5PmcQ38CKG0oT2gdJmJqIxNmAEHkatYGHcMDtXRBpOhOSdraFj6SmPyHEmLB
ishaq7Jm8NPPNK9QcEQ3q+ERa5M6eM72PpF93g2p5cjKgyzzfoIV09Zb/LJ2aW2g
QwIDAQAB
-----END PUBLIC KEY-----
';
}
echo 'RSA 2048 encryption OAEP SHA-256 string' . PHP_EOL;
$dataToEncryptString = "The quick brown fox jumps over the lazy dog";
echo 'plaintext: ' . $dataToEncryptString . PHP_EOL;
// # # # usually we would load the private and public key from a file or keystore # # #
// # # # here we use hardcoded keys for demonstration - don't do this in real programs # # #
// encryption
echo PHP_EOL . '* * * encrypt the plaintext with the RSA public key * * *' .PHP_EOL;
$ciphertextBase64 = base64Encoding(rsaEncryptionOaepSha256(loadRsaPublicKeyPem(), $dataToEncryptString));
echo 'ciphertextBase64: ' . $ciphertextBase64 . PHP_EOL;
// transport the encrypted data to recipient
// receiving the encrypted data, decryption
echo PHP_EOL . '* * * decrypt the ciphertext with the RSA private key * * *' .PHP_EOL;
$ciphertextReceivedBase64 = $ciphertextBase64;
echo 'ciphertextReceivedBase64: ' . $ciphertextReceivedBase64 . PHP_EOL;
$decryptedtext = rsaDecryptionOaepSha256(loadRsaPrivateKeyPem(), base64Decoding($ciphertextReceivedBase64));
echo 'decryptedtext: ' . $decryptedtext . PHP_EOL;
?>