pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Moreno Andreo <moreno.andreo@evolu-s.it>
To: pgsql-general@lists.postgresql.org
Subject: Re: pgp_sym_decrypt() - error 39000: wrong key or corrupt data
Date: Fri, 22 Jun 2018 06:18:08 -0700
Message-ID: <bed422e7-533c-42af-e9d1-574f8961747e@aklaver.com> (raw)
In-Reply-To: <f4f6f8cf-eb26-d5a2-97d9-3303389c392b@evolu-s.it>
References: <1d7de609-5738-bf51-f0b9-b32a889f04e1@evolu-s.it>
	<94ecaa31-5960-6264-81f9-c8560a3f9fed@aklaver.com>
	<f4f6f8cf-eb26-d5a2-97d9-3303389c392b@evolu-s.it>

On 06/22/2018 01:46 AM, Moreno Andreo wrote:
> Il 21/06/2018 23:31, Adrian Klaver ha scritto:
>> On 06/21/2018 08:36 AM, Moreno Andreo wrote:
>>> Hi,
>>>      while playing with pgcrypto I ran into a strange issue 
>>> (postgresql 9.5.3 x86 on Windows 7)
>>>
>>> Having a table with a field
>>> dateofbirth text
>>>
>>> I made the following sequence of SQL commands
>>> update tbl_p set dateofbirth = pgp_sym_encrypt('2018-06-21', 
>>> 'AES_KEY') where codguid = '00000001-0001-0001-0001-000000000001';
>>> OK
>>>
>>> select pgp_sym_decrypt(dateofbirth::bytea, 'AES_KEY') as datanasc 
>>> from tbl_p where codguid = '00000001-0001-0001-0001-000000000001'
>>> '2018-06-21'
>>>
>>> select * from tab_paz where pgp_sym_decrypt(natoil::bytea, 'AES_KEY') 
>>> = '2018-06-21'
>>
>> You switched gears above.
>>
>> What is the data type of the natoil field in table tab_paz?
> Sorry, just a typo... natoil is, actually dateofbirth, so it's text.
> You can read it as
> select * from tbl_p where pgp_sym_decrypt(dateofbirth::bytea, 'AES_KEY') 
> = '2018-06-21'
>>
>> Was the data encrypted in it using the 'AES_KEY'?
> Yes, the command sequence is exactly reported above.
> If I use pgp_sym_decrypt in a SELECT statement it's OK, but if it's in a 
> where clause it seems not to be working.

Are you sure that the entries where not encrypted with a different key 
because I can't replicate.(More comments below):

create table pgp_test(id integer, fld_1 varchar);

insert  into pgp_test values (1, pgp_sym_encrypt('2018-06-21', 'AES_KEY'))

select * from pgp_test ;

  id | 
         fld_1 

----+------------------------------------------------------------------------------------------------------------------------------------------------------------
   1 | 
\xc30d04070302444e9b2792436e3d7ed23b01cf097f0a6a36298bab63ae4f22f39de54a9b0d8f905d48198ce76089de5f21669c46d96439718b6b0408c541427b6e7c11008bd3d0ebdae0dceb

select * from pgp_test where pgp_sym_decrypt(fld_1::bytea, 'AES_KEY') = 
'2018-06-21';

id | 
       fld_1 

----+------------------------------------------------------------------------------------------------------------------------------------------------------------
   1 | 
\xc30d04070302444e9b2792436e3d7ed23b01cf097f0a6a36298bab63ae4f22f39de54a9b0d8f905d48198ce76089de5f21669c46d96439718b6b0408c541427b6e7c11008bd3d0ebdae0dceb


Have you looked at the entry in its encrypted state to see if it looks 
the same as pgp_sym_encrypt('2018-06-21', 'AES_KEY')?

Can you return decrypted values for other items in the table?


> 
> 
> 
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com




view thread (8+ messages)  latest in thread

Message-ID: <bed422e7-533c-42af-e9d1-574f8961747e@aklaver.com>
Permalink:  ../bed422e7-533c-42af-e9d1-574f8961747e@aklaver.com/
Also on:    postgresql.org/message-id/bed422e7-533c-42af-e9d1-574f8961747e@aklaver.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: adrian.klaver@aklaver.com, moreno.andreo@evolu-s.it
  Subject: Re: pgp_sym_decrypt() - error 39000: wrong key or corrupt data
  In-Reply-To: <bed422e7-533c-42af-e9d1-574f8961747e@aklaver.com>
  References: <1d7de609-5738-bf51-f0b9-b32a889f04e1@evolu-s.it> <94ecaa31-5960-6264-81f9-c8560a3f9fed@aklaver.com> <f4f6f8cf-eb26-d5a2-97d9-3303389c392b@evolu-s.it> <bed422e7-533c-42af-e9d1-574f8961747e@aklaver.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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