Rumah > Artikel > pembangunan bahagian belakang > Cara Menukar Penyulitan Mcrypt kepada Penyulitan OpenSSL: Panduan Langkah demi Langkah
Mcrypt, a legacy encryption library, has seen its retirement due to security vulnerabilities and the emergence of OpenSSL as the industry standard. While switching from Mcrypt to OpenSSL may seem daunting, it can be simplified by understanding the key differences and adopting appropriate strategies.
Blowfish, the cipher you're using, requires data padding for encryption. OpenSSL employs PKCS#7 padding, while Mcrypt utilizes PKCS#5. PKCS#7 requires a minimum padding length of 1, while PKCS#5 accepts 0. This difference affects the encrypted results obtained from each library.
In ECB mode, which you're employing, an initialization vector (IV) is not required. However, this is not the case in other encryption modes. Ensure you set the IV length to 0 when using OpenSSL for ECB mode.
To decrypt data encrypted with Mcrypt using OpenSSL, manual padding with PKCS#7 is required prior to encryption. This ensures compatibility between the two libraries.
The provided code demonstrates the key differences and proper padding techniques:
$key = "anotherpassword1"; $str = "does it work 12"; // Mcrypt padding $enc = mcrypt_encrypt(MCRYPT_BLOWFISH, $key, $str."\1", MCRYPT_MODE_ECB); // OpenSSL padding $enc = openssl_encrypt($str, 'bf-ecb', $key, true); // Decrypt using both methods $dec_mcrypt = mcrypt_decrypt(MCRYPT_BLOWFISH, $key, $enc, MCRYPT_MODE_ECB); $dec_openssl = openssl_decrypt($enc, 'bf-ecb', $key, true); echo bin2hex($enc) . PHP_EOL; var_dump($dec_mcrypt); echo PHP_EOL; var_dump($dec_openssl);
This code demonstrates successful encryption and decryption using both Mcrypt and OpenSSL, applying PKCS#7 padding for compatibility.
Atas ialah kandungan terperinci Cara Menukar Penyulitan Mcrypt kepada Penyulitan OpenSSL: Panduan Langkah demi Langkah. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!