encrypt/decrypt sqlite data

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This is my first adventure with mcrypt and also the sqlite stuff.

Via file upload I am getting a SQL dump file and running it as a query
to insert data into a sqlite db.  This works great.

Then I am trying to use an update query to encrypt fields in all rows by
using the sqlite_create_function to run my encryption function that uses $_SESSION['key'] which is an md5 hash of a pass phrase):


	$db = sqlite_open("db");	
	sqlite_create_function($db, 'enc', 'encrypt', 1);
		
	$sql  = 'UPDATE mytable SET f1=enc(f1);'
	.'UPDATE results SET f2=enc(f2);';

	sqlite_query($db, $sql);

function encrypt($txt)
{
	$key = $_SESSION['key'];
	$txt = trim($txt);
	
	$iv_size = mcrypt_get_iv_size(MCRYPT_CAST_256, MCRYPT_MODE_ECB);
	$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
	
	$black = mcrypt_encrypt(MCRYPT_CAST_256, $key, $txt, MCRYPT_MODE_ECB, $iv);
	
	return $black;
}

Then to test, I query the db and run each field of data thru a decrypt
function before displaying it:

	$db = sqlite_open("db");
	$sql = "SELECT * from mytable";
	$row = sqlite_array_query($db, $sql);
	
	echo "<table width=\"100%\" border=\"1\">";
	foreach ($row as $k => $v) {
		echo "<tr>"
		."<td>".decrypt($v['f1'])."&nbsp;</td>"
		."<td>".decrypt($v['f2'])."&nbsp;</td>"
		."</tr>";
	}
	echo "</table>";

function decrypt($black)
{
	$key = $_SESSION['key'];
	
	$iv_size = mcrypt_get_iv_size(MCRYPT_CAST_256, MCRYPT_MODE_ECB);
	$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
	
	$txt = mcrypt_decrypt(MCRYPT_CAST_256, $key, $black, MCRYPT_MODE_ECB, $iv);
	
	return $txt;
}

The problem is that the data that is displayed is not the original data.
Some fields seem to be truncated and some seem to be mostly decrypted
except for the last line or so. For the fields f1 and f2 I have tried
text and blob. The actually data is either one word of text or free flow text with line feeds.


TIA,
Shawn

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux