pg.ddx.io  pgsql-php@postgresql.org mailing list archive  
help / color / mirror / Atom feed
pgcrypto & php
2+ messages / 1 participants
[nested] [flat]

* pgcrypto & php
@ 2004-01-12 10:34  Daniel Struck <struck.d@retrovirology.lu>
  0 siblings, 1 reply; 2+ messages in thread

From: Daniel Struck @ 2004-01-12 10:34 UTC (permalink / raw)
  To: pgsql-php

Hello,


I am storing certain data encrypted by pgcrypto in my database.

Here are the commands I use to test the functionality:

CREATE TABLE crypto (
id              SERIAL PRIMARY KEY,
title           VARCHAR(50),
crypted_content BYTEA
);


INSERT INTO  crypto VALUES (1,'test1',encrypt('daniel', 'fooz', 'aes'));
INSERT INTO  crypto VALUES (2,'test2',encrypt('struck', 'fooz', 'aes'));
INSERT INTO  crypto VALUES (3,'test3',encrypt('konz', 'fooz', 'aes'));

SELECT * FROM crypto;

SELECT *,decrypt(crypted_content, 'fooz', 'aes') FROM crypto;

SELECT *,decrypt(crypted_content, 'fooz', 'aes') FROM crypto WHERE decrypt(crypted_content, 'fooz', 'aes')='struck';


There are certain occasions where I would like to be able to decrypt/ encrypt the data in PHP itself. Is this possible with the "Mcrypt Encryption Functions" of PHP?

I have already tried it, by as I am no expert in cryptography, I have difficulties finding the right syntax.
This is the code I used to try it out:

$test=new db_sql_user;
$test->query("select crypted_content from crypto WHERE id=1;");
$crypt=$test->result();

echo "<br />";
echo "crypted content from postgresql: " .$crypt;
$array = unpack("c2chars/nint", $crypt);
echo $array;
echo "<br />";
echo mcrypt_decrypt ( MCRYPT_RIJNDAEL_128 , "fooz", $crypt, cbc);
echo "<br />";
echo  mcrypt_cbc (MCRYPT_RIJNDAEL_128, "fooz", $crypt , MCRYPT_DECRYPT);


This is the output:

crypted content from postgresql: g°ñ\220399ùû¹qyg®Û~Array
5*X]Nl\x{2022}u
5*X]Nl\x{2022}u


Best regards,
Daniel Struck


-- 
Retrovirology Laboratory Luxembourg
Centre Hospitalier de Luxembourg
4, rue E. Barblé
L-1210 Luxembourg

phone: +352-44116105
fax:   +352-44116113
web: http://www.retrovirology.lu
e-mail: struck.d@retrovirology.lu



^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: pgcrypto & php
@ 2004-01-12 14:35  Daniel Struck <struck.d@retrovirology.lu>
  parent: Daniel Struck <struck.d@retrovirology.lu>
  0 siblings, 0 replies; 2+ messages in thread

From: Daniel Struck @ 2004-01-12 14:35 UTC (permalink / raw)
  To: pgsql-php

Finally, it works :-)

$crypt=$test->result();
echo "<br />";
echo "crypted content from postgresql: " .$crypt;
echo "<br />";
$crypt=pg_unescape_bytea($crypt);
echo $crypt;
echo "<br />";
echo mcrypt_decrypt ( MCRYPT_RIJNDAEL_128 , "fooz", $crypt, cbc);
echo "<br />";
echo  mcrypt_cbc (MCRYPT_RIJNDAEL_128, "fooz", $crypt , MCRYPT_DECRYPT);

OUTPUT:

crypted content from postgresql: g°ñ\220399ùû¹qyg®Û~
g°ñ399ùû¹qyg®Û~
daniel
daniel 


You have to use the function "pg_unescape_bytea();" from PHP when you extract bytea data from postgresql. How stupid of me, should have known that the bytea data would be escaped ;-)

Daniel



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2004-01-12 14:35 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2004-01-12 10:34 pgcrypto & php Daniel Struck <struck.d@retrovirology.lu>
2004-01-12 14:35 ` Daniel Struck <struck.d@retrovirology.lu>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox