Hi

using this as an example, what is the best way to encrypt and then decrypt data stored in an sqlite database ??

<?php

  try

  {

//open the database

$db = new PDO('sqlite:dogsDb_PDO.sqlite');



//create the database

$db->exec("CREATE TABLE Dogs (Id INTEGER PRIMARY KEY, Breed TEXT, Name TEXT, Age INTEGER)");



//insert some data...

$db->exec("INSERT INTO Dogs (Breed, Name, Age) VALUES ('Labrador', 'Tank', 2);".

           "INSERT INTO Dogs (Breed, Name, Age) VALUES ('Husky', 'Glacier', 7); " .

           "INSERT INTO Dogs (Breed, Name, Age) VALUES ('Golden-Doodle', 'Ellie', 4);");



//now output the data to a simple html table...

print "<table border=1>";

print "<tr><td>Id</td><td>Breed</td><td>Name</td><td>Age</td></tr>";

$result = $db->query('SELECT * FROM Dogs');

foreach($result as $row)

{

  print "<tr><td>".$row['Id']."</td>";

  print "<td>".$row['Breed']."</td>";

  print "<td>".$row['Name']."</td>";

  print "<td>".$row['Age']."</td></tr>";

}

print "</table>";



// close the database connection

$db = NULL;

  }

  catch(PDOException $e)

  {

print 'Exception : '.$e->getMessage();

  }

?>

Thanks 🙂

    Thanks Bjom..

    That resolves one issue, using crypt() I can't encrypt a variable, save it to an sqlite db and the read it back and decrypt it !!

    I think I need to look at this differently.

    I'll start a new thread in coding as I think that will be more appropriate.

    Thanks for the link and your advice.

      That's because crypt isn't encryption but a hashing function. I've never used mcrypt myself, but perhaps it's suitable for your needs.

        Jo is right, it's a hash function.

        That's all described pretty well in the tutorial. I don't mean to lecture you, but since you go through all the trouble of securing the data with encryption in the first place, I would suggest that you take the extra time for some thorough reading on encryption and related topics, just to avoid some common pitfalls. There are quite a few links at the bottom of that tutorial that provide additional info.

        The one in the middle 'Read about PHP's encryption functionality at ONLamp.com.' is pretty much the most straightforward and will lead you to mcrypt in the end. (see Jo's link).

        have fun!

        Bjom

          Write a Reply...