I have a PHP script that will encrypt data to store in a database, but when I run the script, the openssl_encrypt returns false with the error: "error:1C800066:Provider routines::cipher operation failed".
$cipher = str_replace("n", "", file_get_contents("/key/cipher"));
$iv_length = openssl_cipher_iv_length($cipher);
$options = 0;
$iv = str_replace("n", "", file_get_contents("/key/iv"));
$key = str_replace("n", "", file_get_contents("/key/key"));
$firstName = openssl_encrypt($data["firstName"], $cipher, $key, $options, $iv);
I have verified that all of these files exist on my pc, as well as $data["firstName"]
is actually received.
The data that fails is a first name about 6 characters long, but when I type in an email, which is about 19 characters long, it works perfectly fine.
2
Answers
Even though the cipher I was using aes-256-xts, which is on the list when I call the
openssl_get_cipher_methods();
function, I switched to aes-256-ctr and it started working.The error you are encountering, "error:1C800066:Provider routines::cipher operation failed", suggests that the OpenSSL encryption operation is failing due to an issue with the provided parameters or the environment setup. Here are a few steps to troubleshoot and resolve the issue:
Check the Lengths and Validity of IV and Key:
Verify Cipher, IV, and Key:
Debug Output:
Here’s an improved version of your code with added debugging statements:
Explanation:
Debug Output:
Check IV Length:
OpenSSL Error Output:
Additional Considerations:
openssl_get_cipher_methods()
.Here’s an example to list supported ciphers:
/key/cipher
,/key/iv
, and/key/key
files contain the correct data without any extra characters or formatting issues.