Greetings Stack Overflow community,
I'm working on a website that will input data into a Database, and the data itself is rather sensitive. As a precaution I want to have some Encryption / Decryption occur, and while I have had success in some basic testing, it's not currently working when I try to pull the data from the Database itself.
Here is what I currently have working;
$plaintext = 'Hello World';
$encrypted = openssl_encrypt($plaintext, $cipher, $key, 0, $iv);
$decrypted = openssl_decrypt($encrypted, $cipher, $key, 0, $iv);
echo "<pre>" . $encrypted . " - Encrypted Version </pre>";
echo "<pre>" . $decrypted . " - Decrypted Version </pre>";
This works just fine on the page itself (Image) as you can see in this image. When trying to input it into the Database, and retrieve it and Decrypt however, it stops working.
I can see the data inside the Database is encrypted, and I can echo out the results normally, however, when I attempt to use a mysqli_fetch_array()
to loop through and retrieve the data, the same functionality that works just above, no longer works..
while($row = mysqli_fetch_array($result))
{
$title = openssl_decrypt($row['title'], $cipher, $key, 0, $iv);
echo "<pre>" . $title . " - Decrypted Version (MySQLi) </pre>";
}
Do note that the connection to the Database for the loop is working, as I can output the raw data itself. It's just through the openssl_decrypt
that it no longer works.
I'm stumped for what's the cause, I have looked through a plethora of Stack Overflow questions on this, and many other online sources, but none of them are related to my problem.
I thank you for taking the time to read, and look forward to working through this issue.
Additional Information:
Php Version: 7.1.9 MySQL Version: 5.7.14 Apache Version: 2.4.23