Re: Backslah in encrypt function.

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: Nalin Bakshi <nbakshi(at)bisil(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Backslah in encrypt function.
Date: 2007-07-25 14:41:22
Message-ID: 20070725144122.GA66622@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, Jul 25, 2007 at 06:02:10PM +0530, Nalin Bakshi wrote:
> I have come on a problem regarding encryption. I am firing a simple
> select statement:
>
> select encrypt('\\','abcd','bf');
>
> I want to use \ for encryption but I get the error:
> "invalid input syntax for type bytea"
>
> I tried using \\\\ to encrypt \ , but on decryption I get \\ instead of
> \ (single backslash).

The double backslash is the output representation of a single
backslash. See Table 8-7 "bytea Literal Escaped Octets" and Table
8-8 "bytea Output Escaped Octets" in the documentation:

http://www.postgresql.org/docs/8.2/interactive/datatype-binary.html

You can use length(), octet_length(), or encode() to see that the
decrypted value contains only a single octet:

test=> select decrypt(encrypt(e'\\\\', 'abcd', 'bf'), 'abcd', 'bf');
decrypt
---------
\\
(1 row)

test=> select octet_length(decrypt(encrypt(e'\\\\', 'abcd', 'bf'), 'abcd', 'bf'));
octet_length
--------------
1
(1 row)

test=> select encode(decrypt(encrypt(e'\\\\', 'abcd', 'bf'), 'abcd', 'bf'), 'hex');
encode
--------
5c
(1 row)

Depending on your security requirements you might wish to use
pgp_sym_encrypt() or pgp_sym_encrypt_bytea() instead of encrypt().
See the "Raw encryption" section of README.pgcrypto for some of the
disadvantages of encrypt().

--
Michael Fuhr

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Alvaro Herrera 2007-07-25 15:46:09 Re: a few questions (and doubts) about xid
Previous Message Gregory Stark 2007-07-25 14:09:27 Re: a few questions (and doubts) about xid