Upgrading my encryption library from Mcrypt to OpenSSL
Question:
Is it possible to upgrade my encryption library from Mcrypt to OpenSSL and retain the ability to decrypt data that was encrypted using Mcrypt?
Conflicting Information:
There appears to be conflicting information online regarding the compatibility between these two libraries.
Additional Context:
I am attempting to convert an encryption class that currently uses Mcrypt to one that uses OpenSSL. However, I am encountering difficulties in decrypting data encrypted with the Mcrypt version.
Working Code for Decryption:
The following revised code for the decryption routine in the OpenSSL version has been confirmed to work:
public function decrypt($data, $key) { $salt = substr($data, 0, 128); $enc = substr($data, 128, -64); $mac = substr($data, -64); list ($cipherKey, $macKey, $iv) = $this->getKeys($salt, $key); if ($mac !== hash_hmac('sha512', $enc, $macKey, true)) { return false; } $dec = openssl_decrypt($enc, $this->cipher, $cipherKey, OPENSSL_RAW_DATA, $iv); return $dec; }
Test Results:
Testing this revised code with various data and keys yielded no failures.
Conclusion:
It is possible to upgrade the encryption library from Mcrypt to OpenSSL and successfully decrypt data that was encrypted with the previous library, provided that you use the updated decryption routine.
The above is the detailed content of Can I Migrate from Mcrypt to OpenSSL Encryption While Maintaining Decryption Compatibility?. For more information, please follow other related articles on the PHP Chinese website!