I am posting data from a form and encrypting it using a key (stored in mykey.php as $key) using the following functions(stored in functions.php file).
<?php
include '../../mykey.php';
//ENCRYPT FUNCTION
function encryptthis($data, $key) {
$encryption_key = base64_decode($key);
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));
$encrypted = openssl_encrypt($data, 'aes-256-cbc', $encryption_key, 0, $iv);
return base64_encode($encrypted . '::' . $iv);
}
//DECRYPT FUNCTION
function decryptthis($data, $key) {
$encryption_key = base64_decode($key);
list($encrypted_data, $iv) = array_pad(explode('::', base64_decode($data), 2),2,null);
return openssl_decrypt($encrypted_data, 'aes-256-cbc', $encryption_key, 0, $iv);
}
?>
The data is posted to the database successfully and the column data is encrypted. I am now trying to retrieve data by entering an email address into a form and retrieving all records related to that email.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Find the required post values
$email = $_POST['email'];
Then in my query I'm trying to select using the decrypted values. However no records are returned It worked fine when there was no encryption (also only some fields are encrypted).
Here is all of the code
include './functions.php';
include './config.php';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Find the required post values
$table = $_POST['appurlkey'];
$email = $_POST['email'];
try {
// Start connection
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// Set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Query
$sql = "SELECT * FROM $table WHERE addedby_email=:addedby_email AND active=:active ORDER BY id DESC";
// Prepare query
$stmt = $conn->prepare($sql);
// Bind
$stmt->bindValue(':addedby_email', $encemail);
$stmt->bindValue(':active', '1');
// Execute
$stmt->execute();
if ($stmt->rowCount() > 0) {
$msg = "Successfully fetched the list.";
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($rows as $row) {
$myDateTime = DateTime::createFromFormat('Y-m-d H:i:s', $row["created_at"]);
$created_at = $myDateTime->format('d/m/Y');
$data = [
"id" => $row["id"],
"addedby_email" => $row["email"] ,
"addedby_name" => decryptthis($row["addedby_name"],$key),
"firstname" => decryptthis($row["firstname"],$key),
"lastname" => decryptthis($row["lastname"],$key),
"birthdate" => $row["birthdate"],
"phone" => decryptthis($row["phone"],$key),
"email" => decryptthis($row["email"],$key),
"address" => decryptthis($row["address"],$key),
"town" => $row["town"],
"county" => $row["county"],
"postcode" => decryptthis($row["postcode"],$key),
"active" => $row["active"],
"created_at" => $created_at
];
$persons[] = [
"pt_id" => $row["id"],
"name" => decryptthis($row["firstname"],$key) . " " . decryptthis($row["lastname"],$key),
"birthdate" => $row["birthdate"],
"data" => $data,
];
}
} else {
$msg = "No person found.";
$persons = null;
}
$response = [
"success" => true,
"message" => $msg,
"persons" => $persons,
];
} catch(PDOException $e) {
$msg = "Error while fetching the persons list.";
// $msg = $sql . "<br>" . $e->getMessage();
$response = [
"success" => false,
"message" => $msg,
"persons" => null,
];
}
// Close connection
$conn = null;
// Json response
echo json_encode($response);
}