store files encrypted with pgcrypto

store files encrypted with pgcrypto

am 21.10.2008 18:03:28 von elmarkivp

This is a multi-part message in MIME format.

------=_NextPart_000_0013_01C93385.CB3B7C30
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Hi, i'm trying to store files encrypted in postgres 8.3.4, using =
sym_encrypt_bytea function.
this is quoted from the postgres8.3 documentation, thats was my guide, =
below is the php code i have used to make it, with no results and the =
respective errors.

FROM http://www.postgresql.org/docs/8.3/static/pgcrypto.html

F.20.3.1. pgp_sym_encrypt()

pgp_sym_encrypt(data text, psw text [, options text ]) returns bytea
pgp_sym_encrypt_bytea(data bytea, psw text [, options text ]) =
returns bytea
=20

Encrypt data with a symmetric PGP key psw. The options parameter can =
contain option settings, as described below.
F.20.3.2. pgp_sym_decrypt()

pgp_sym_decrypt(msg bytea, psw text [, options text ]) returns text
pgp_sym_decrypt_bytea(msg bytea, psw text [, options text ]) returns =
bytea
=20

Decrypt a symmetric-key-encrypted PGP message.

Decrypting bytea data with pgp_sym_decrypt is disallowed. This is to =
avoid outputting invalid character data. Decrypting originally textual =
data with pgp_sym_decrypt_bytea is fine.

The options parameter can contain option settings, as described below.=20

F.20.3.7. Options for PGP functions

Options are named to be similar to GnuPG. An option's value should be =
given after an equal sign; separate options from each other with commas. =
For example:

pgp_sym_encrypt(data, psw, 'compress-algo=3D1, =
cipher-algo=3Daes256')
=20

All of the options except convert-crlf apply only to encrypt functions. =
Decrypt functions get the parameters from the PGP data.

The most interesting options are probably compress-algo and =
unicode-mode. The rest should have reasonable defaults.=20

THIS IS MY CODE..
=20
$fp =3D fopen($tmp_name, "rb");
$buffer =3D fread($fp, filesize($tmp_name));
fclose($fp);
$buffer=3Dpg_escape_bytea($buffer);
$sql =3D "INSERT INTO foo(nombre, descripcion, archivo_bytea, mime, =
size) VALUES ('$nombre', '$desc', =
pgp_sym_encrypt_bytea('$buffer',123,'cipher-algo=3Daes256'), '$type', =
$size)";
?>

ERRORS: =20
ERROR: No function matches the given name and argument types. You =
might need to add explicit type casts=20
ERROR: function pgp_sym_encrypt_bytea(unknown, integer, unknown) does =
not exist=20
=20
OTHER CODE WITH EXPLICIT TYPES
=20
$fp =3D fopen($tmp_name, "rb");
$buffer =3D fread($fp, filesize($tmp_name));
fclose($fp);
$buffer=3Dpg_escape_bytea($buffer);
$sql =3D "INSERT INTO foo(nombre, descripcion, archivo_bytea, mime, =
size) VALUES ('$nombre', '$desc', =
pgp_sym_encrypt_bytea('$buffer'::bytea,123::text,'cipher-alg o=3Daes256'::=
text), '$type', $size)";
?>

ERRORS:
ERROR: function pgp_sym_encrypt_bytea(bytea, text, text) does not =
exist=20
=20
any help is welcome!
=20
=20

------=_NextPart_000_0013_01C93385.CB3B7C30
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable



charset=3Diso-8859-1">




Hi, i'm trying to store files encrypted =
in postgres=20
8.3.4, using sym_encrypt_bytea function.
this is quoted from the =
postgres8.3=20
documentation, thats was my guide, below is the php code i have used to =
make it,=20
with no results and the respective errors.

 

FROM href=3D"http://www.postgresql.org/docs/8.3/static/pgcrypto.h tml">http://w=
ww.postgresql.org/docs/8.3/static/pgcrypto.html

 

F.20.3.1. =
pgp_sym_encrypt()

 

    pgp_sym_encrypt(data =
text, psw=20
text [, options text ]) returns bytea
   =20
pgp_sym_encrypt_bytea(data bytea, psw text [, options text ]) returns=20
bytea
  

 

Encrypt data with a symmetric PGP key =
psw. The=20
options parameter can contain option settings, as described =
below.
F.20.3.2.=20
pgp_sym_decrypt()

 

    pgp_sym_decrypt(msg =
bytea, psw=20
text [, options text ]) returns text
   =20
pgp_sym_decrypt_bytea(msg bytea, psw text [, options text ]) returns=20
bytea
  

 

Decrypt a symmetric-key-encrypted PGP=20
message.

 

Decrypting bytea data with =
pgp_sym_decrypt is=20
disallowed. This is to avoid outputting invalid character data. =
Decrypting=20
originally textual data with pgp_sym_decrypt_bytea is fine.

 

The options parameter can contain =
option settings,=20
as described below.

 

F.20.3.7. Options for PGP =
functions

 

Options are named to be similar to =
GnuPG. An=20
option's value should be given after an equal sign; separate options =
from each=20
other with commas. For example:

 

    =
pgp_sym_encrypt(data, psw,=20
'compress-algo=3D1, cipher-algo=3Daes256')
  

 

All of the options except convert-crlf =
apply only=20
to encrypt functions. Decrypt functions get the parameters from the PGP=20
data.

 

The most interesting options are =
probably=20
compress-algo and unicode-mode. The rest should have reasonable =
defaults.=20

 

THIS IS MY=20
CODE..
 
  <?php
  $fp =3D =
fopen($tmp_name,=20
"rb");
     $buffer =3D fread($fp,=20
filesize($tmp_name));
  fclose($fp);
  $buffer=3D=
pg_escape_bytea($buffer);
  $sql=20
=3D "INSERT INTO foo(nombre, descripcion, archivo_bytea, mime, =
size) VALUES=20
('$nombre', '$desc', =
pgp_sym_encrypt_bytea('$buffer',123,'cipher-algo=3Daes256'), =20
'$type', $size)";
  ?>

 

size=3D2>ERRORS:  
  ERROR: No=20
function matches the given name and argument types. You might need to =
add=20
explicit type casts 
  ERROR: function=20
pgp_sym_encrypt_bytea(unknown, integer, unknown) does not exist=20

  
OTHER CODE WITH EXPLICIT=20
TYPES
  
  <?php
  $fp =
fopen($tmp_name, "rb");
     $buffer =3D =
fread($fp,=20
filesize($tmp_name));
  fclose($fp);
  $buffer=3D=
pg_escape_bytea($buffer);
  $sql=20
=3D "INSERT INTO foo(nombre, descripcion, archivo_bytea, mime, =
size) VALUES=20
('$nombre', '$desc',=20
pgp_sym_encrypt_bytea('$buffer'::bytea,123::text,'cipher-alg o=3Daes256'::=
text),=20
'$type', $size)";
  ?>

 

ERRORS:
  ERROR: function=20
pgp_sym_encrypt_bytea(bytea, text, text) does not=20
exist 
  

any help is=20
welcome!
  
  


------=_NextPart_000_0013_01C93385.CB3B7C30--

Re: store files encrypted with pgcrypto

am 21.10.2008 23:56:58 von dmagick

elmarkivp wrote:
> Hi, i'm trying to store files encrypted in postgres 8.3.4, using
> sym_encrypt_bytea function.
> this is quoted from the postgres8.3 documentation, thats was my guide,
> below is the php code i have used to make it, with no results and the
> respective errors.
>
> FROM http://www.postgresql.org/docs/8.3/static/pgcrypto.html
>
> F.20.3.1. pgp_sym_encrypt()
>
> pgp_sym_encrypt(data text, psw text [, options text ]) returns bytea
> pgp_sym_encrypt_bytea(data bytea, psw text [, options text ])
> returns bytea
>
>
> Encrypt data with a symmetric PGP key psw. The options parameter can
> contain option settings, as described below.
> F.20.3.2. pgp_sym_decrypt()
>
> pgp_sym_decrypt(msg bytea, psw text [, options text ]) returns text
> pgp_sym_decrypt_bytea(msg bytea, psw text [, options text ]) returns
> bytea
>



> ERRORS:
> ERROR: No function matches the given name and argument types. You
> might need to add explicit type casts
> ERROR: function pgp_sym_encrypt_bytea(unknown, integer, unknown) does
> not exist

pgcrypto is a contrib module in pg 8.3 (ie not built in by default). Did
you install the module?

There are instructions on how to do that here:

http://www.postgresql.org/docs/8.3/static/contrib.html

--
Postgresql & php tutorials
http://www.designmagick.com/


--
Sent via pgsql-php mailing list (pgsql-php@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-php