pgcrypto functions fail for asymmetric encryption/decryption - Mailing list pgsql-general

From Stefan Niantschur
Subject pgcrypto functions fail for asymmetric encryption/decryption
Date
Msg-id 200711291723.26068.sniantschur@web.de
Whole thread Raw
Responses Re: pgcrypto functions fail for asymmetric encryption/decryption  ("Marko Kreen" <markokr@gmail.com>)
Re: pgcrypto functions fail for asymmetric encryption/decryption  ("Marko Kreen" <markokr@gmail.com>)
List pgsql-general
Hi,

I have a table with userids and public keys. I want to write a function
which does a select and returns the result pgp encrypted.

However, I have some problems:
SELECT encode(decode((SELECT ens_pubkey FROM
ens_user)::text,'escape'),'escape'::text)::text;
-> returns the public key, => ok

SELECT armor(dearmor((SELECT ens_pubkey FROM ens_user)::text));
-> returns the key in a different format, => problem

SELECT
armor(pgp_pub_encrypt_bytea(armor(pgp_sym_encrypt('geheim'::text,'test'::text))::bytea,dearmor((SELECT
ens_pubkey FROM ens_user WHERE ens_userid = 10112)::text)));
-> returns a pgp-encrypted message which cannot be decrypted by GnuPG,
=> problem

SELECT
pgp_pub_decrypt(dearmor(armor(pgp_pub_encrypt(armor(pgp_sym_encrypt('geheim'::text,'test'::text)),dearmor((SELECT
ens_pubkey FROM ens_user WHERE ens_userid =
10112)::text)))),dearmor((SELECT ens_privkey FROM ens_user WHERE
ens_userid = 10112)::text),'test'::text);
-> returns 'ERROR: Corrupt data' => problem

SELECT
pgp_key_id(pgp_pub_encrypt_bytea(armor(pgp_sym_encrypt('geheim'::text,'test'::text))::bytea,dearmor((SELECT
ens_pubkey FROM ens_user WHERE ens_userid = 10112)::text)));
-> returns the correct key id of the deployed public key

So, if I cannot decrypt the message which I have been encrypting with
the appropriate keys, how can I proceed?

I want to encrypt messages in postgres and decrypt it elsewhere,
However, the result of the encryption algorithm seems to deliver a
wrong result. Otherwise I cannot explain why encrypting and immidiately
decrypting the message fails.

The same proceeding is succesful when using symmetric keys:
SELECT
pgp_sym_decrypt((pgp_sym_encrypt('geheim'::text,'test'::text)),'test'::text);
-> returns 'geheim' which is the encrypted and then again decrypted
message.

What did I wrong when trying to use asymmetric encryption?

tia

pgsql-general by date:

Previous
From: "Andrus"
Date:
Subject: log_line_prefix='%t %u %d %h %p %i %l %x ' causes error
Next
From: "Ragnar Heil"
Date:
Subject: Postgres High Availablity Solution needed for hot-standby and load balancing