下面是使用这三种算法进行加密和解密的示例代码:
// 生成RSA密钥对
$keyPair = openssl_pkey_new(array('private_key_bits' => 2048,'private_key_type' => OPENSSL_KEYTYPE_RSA,
));// 获取私钥和公钥
openssl_pkey_export($keyPair, $privateKey);
$publicKey = openssl_pkey_get_details($keyPair)['key'];// 要加密的数据
$data = "Hello, World!";// 使用MD5RSA进行加密和解密
$encryptedMD5RSA = '';
openssl_public_encrypt($data, $encryptedMD5RSA, $publicKey, OPENSSL_PKCS1_PADDING);
$decryptedMD5RSA = '';
openssl_private_decrypt($encryptedMD5RSA, $decryptedMD5RSA, $privateKey, OPENSSL_PKCS1_PADDING);// 使用SHA1RSA进行加密和解密
$encryptedSHA1RSA = '';
openssl_public_encrypt($data, $encryptedSHA1RSA, $publicKey, OPENSSL_PKCS1_PADDING);
$decryptedSHA1RSA = '';
openssl_private_decrypt($encryptedSHA1RSA, $decryptedSHA1RSA, $privateKey, OPENSSL_PKCS1_PADDING);// 使用SHA256RSA进行加密和解密
$encryptedSHA256RSA = '';
openssl_public_encrypt($data, $encryptedSHA256RSA, $publicKey, OPENSSL_PKCS1_PADDING);
$decryptedSHA256RSA = '';
openssl_private_decrypt($encryptedSHA256RSA, $decryptedSHA256RSA, $privateKey, OPENSSL_PKCS1_PADDING);// 打印结果
echo "Original Data: $data\n";
echo "MD5RSA Encrypted: " . base64_encode($encryptedMD5RSA) . "\n";
echo "MD5RSA Decrypted: $decryptedMD5RSA\n";
echo "SHA1RSA Encrypted: " . base64_encode($encryptedSHA1RSA) . "\n";
echo "SHA1RSA Decrypted: $decryptedSHA1RSA\n";
echo "SHA256RSA Encrypted: " . base64_encode($encryptedSHA256RSA) . "\n";
echo "SHA256RSA Decrypted: $decryptedSHA256RSA\n";