The compatibility of Mcrypt with OpenSSL is a matter of debate. Some sources claim that OpenSSL cannot decrypt data encrypted using Mcrypt, while others suggest it is possible with the use of padding.
[Post 1](https://stackoverflow.com/a/19748494/5834657) states that decryption is impossible, while [Post 2](https://stackoverflow.com/a/31614770/5834657) suggests that it is achievable with the right padding. However, the example padding provided in the post is specifically meant for Mcrypt and may not be applicable to OpenSSL.
To test the compatibility, we modified the existing Mcrypt code to use OpenSSL. The modified decryption function reads as follows:
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); $data = $this->unpad($dec); return $data; }
We tested this modified code by encrypting a string with the Mcrypt library and then attempting to decrypt it using our OpenSSL-based code. However, we only received a blank response. The error appeared to originate from the $data = $this->unpad($dec) line. When we commented this line out, we got a jumbled string resembling the original encrypted format.
Unfortunately, our attempt to decrypt Mcrypt-encrypted data using OpenSSL was unsuccessful. It is possible that the specific padding used by Mcrypt is not compatible with OpenSSL, or there may be other underlying incompatibilities that prevent successful decryption.
Further investigation is needed to determine if it is truly impossible to decrypt Mcrypt-encrypted data with OpenSSL, or if there is a workaround that has yet to be discovered.
The above is the detailed content of Can OpenSSL Decrypt Data Encrypted with Mcrypt?. For more information, please follow other related articles on the PHP Chinese website!