Shane
Shane

Reputation: 753

Decrypt data from MySQL database

I have the below code to show all data from a MySQL database in a HTMl database:

<?php
$result = mysqli_query($con,"SELECT * FROM Persons");
echo "<table border='1'>";
$i = 0;
while($row = $result->fetch_assoc())
{
    if ($i == 0) {
      $i++;
      echo "<tr>";
      foreach ($row as $key => $value) {
        echo "<th>" . $key . "</th>";
      }
      echo "</tr>";
    }
    echo "<tr>";
    foreach ($row as $value) {
      echo "<td>" . $value . "</td>";
    }
    echo "</tr>";
}
echo "</table>";

mysqli_close($con);
?>

This code works fine and the data is displayed in the table correctly, my problem is that most of the data in the DB is encrypted (simply)!

For example, here is how someones first name is stored 5cGs+mhdNN/SnHdqEbMk6zlKRu8uW7pR1zRjQHV9DGQ=

Is there a way to decrypt the data before displaying it in the table?

I usually decrpyt the date in the following way:

$name_firstD = simple_decrypt($name_first , "secured");

Upvotes: 0

Views: 704

Answers (1)

Mojtaba
Mojtaba

Reputation: 5002

You need to have an array of columns which are encrypted.

<?php
$result = mysqli_query($con,"SELECT * FROM Persons");

$encrypted_columns = array('password','code', 'first_name');

echo "<table border='1'>";
$i = 0;
while($row = $result->fetch_assoc())
{
    if ($i == 0) {
      $i++;
      echo "<tr>";
      foreach ($row as $key => $value) {
        echo "<th>" . $key . "</th>";
      }
      echo "</tr>";
    }
    echo "<tr>";
    foreach ($row as $key => $value) {
      echo "<td>" . (in_array($key,$encrypted_columns))? simple_decrypt($value , "secured") : $value . "</td>";
    }
    echo "</tr>";
}
echo "</table>";

mysqli_close($con);
?>

put the name of your encrypted column inside $encrypted_columns array.

Upvotes: 3

Related Questions