Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fWQIe-00079Q-7B for pgsql-general@arkaria.postgresql.org; Fri, 22 Jun 2018 17:56:45 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fWQIc-0008Ci-AF for pgsql-general@arkaria.postgresql.org; Fri, 22 Jun 2018 17:56:42 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fWQIa-0008CM-Ca for pgsql-general@lists.postgresql.org; Fri, 22 Jun 2018 17:56:42 +0000 Received: from out2-smtp.messagingengine.com ([66.111.4.26]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fWQIS-0003lu-FH for pgsql-general@postgresql.org; Fri, 22 Jun 2018 17:56:38 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 738E8215C6; Fri, 22 Jun 2018 13:56:30 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Fri, 22 Jun 2018 13:56:30 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; bh=hF+OFOLzWNEcJjkxp/sTitRXXWsZj KR+BAgnHCK6jWU=; b=syVulSkdEN/qMy7QCcxGgRnyScyjmrnQkVSTv9nkZuIsu YXgrYepSoaiEqsS4qlm0Vd7lezwoCmUCNU2kxk0hutToDc2FEaqLCdwhLCzbZGa+ VEYoChXu8CHcfhryiE51Wlj9Rqr2E/CnUyXEC0/i1eUjKqEFYOPOXNwwfWsCGQub 1HGCgqA6xQ5CSVaO1xXILPnAECzaI+2Mu1vwdLsn28xqeA87Z++iXFKTh8OAHwiL z/KV1X6OycQ1GXba0qVO/HlaK2sqxDcqRBPHvGqReqAeLyTurLvZy5vn6qLEn3wF 9aZEiED0Wr2jgDvbm0KV3F7ivzMOdqAdDXkh0wukg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-sender:x-me-sender:x-sasl-enc; s=fm3; bh=hF+OFO LzWNEcJjkxp/sTitRXXWsZjKR+BAgnHCK6jWU=; b=exB/kOS2FhjtG5WJAA/xFt MdHReqlMj9HCbFWK1aK7cavIPIh4Wti12rEiuuXHdHI45Dq14X4AxE8rajhK1Ort FcSiAkk9kdAPQnp8A5KYH5dDzo28dTsweycAPcCUa2VnIE0fKBLMwexYROsQN23I /eWTGTtVtlMMAPhHiN/sOmLIl3ZAC0yDe0eJL+/M0nBSF6cktDEyh02O646czKDE PncZ7Bc7WBMHf0pUpgAi+9KIcVSMaPuViX1rpq91GRW9JvprN3S9/rKfC/0Wd2Wx 5v+H8Kfcn+ZdzXPECQRQ+kWo/IStFZjcucxX9kE81chN+F1RUfGnFZcUYGbIYZew == X-ME-Proxy: X-ME-Sender: Received: from [192.168.1.3] (unknown [71.212.159.209]) by mail.messagingengine.com (Postfix) with ESMTPA id C9342E4329; Fri, 22 Jun 2018 13:56:29 -0400 (EDT) Subject: Re: pgp_sym_decrypt() - error 39000: wrong key or corrupt data To: Moreno Andreo , PostgreSQL mailing lists References: <1d7de609-5738-bf51-f0b9-b32a889f04e1@evolu-s.it> <94ecaa31-5960-6264-81f9-c8560a3f9fed@aklaver.com> From: Adrian Klaver Message-ID: <6fc4a146-3100-e3b9-e09e-c5bc290b76a8@aklaver.com> Date: Fri, 22 Jun 2018 10:56:28 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=iso-8859-15; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 06/22/2018 09:50 AM, Moreno Andreo wrote: > Il 22/06/2018 15:18, Adrian Klaver ha scritto: >> Are you sure that the entries where not encrypted with a different key >> because I can't replicate.(More comments below): > (other replies below, inline) > I'm almost sure (you're never absolutely sure :-) ), since I kept all > commands I entered in PgAdminIII SQL Window, and they're reported above. > On the other side, I tried the same procedure on another field and it > succeeded. > > The only difference between the 2 fields, and I don't know if it can > make any sense, is that the field I tried now and succeeded was created > as text, while the other field (dateofbirth) was a timestamp I ALTERed > with the statement > alter table tbl_p alter column dateofbirth type text using > to_char(dateofbirth, 'YYYY-MM-DD'); Assuming the ALTER TABLE was done and then the values where encrypted, that does not seem to affect anything here(More below): test=# create table pgp_alter_test(id integer, birthdate date); CREATE TABLE test=# \d pgp_alter_test Table "public.pgp_alter_test" Column | Type | Collation | Nullable | Default -----------+---------+-----------+----------+--------- id | integer | | | birthdate | date | | | test=# insert into pgp_alter_test values (1, '2018-06-21'); INSERT 0 1 test=# select * from pgp_alter_test ; id | birthdate ----+------------ 1 | 2018-06-21 (1 row) test=# alter table pgp_alter_test alter column birthdate type text using to_char(birthdate, 'YYYY-MM-DD'); ALTER TABLE test=# \d pgp_alter_test Table "public.pgp_alter_test" Column | Type | Collation | Nullable | Default -----------+---------+-----------+----------+--------- id | integer | | | birthdate | text | test=# select * from pgp_alter_test ; id | birthdate ----+------------ 1 | 2018-06-21 (1 row) test=# update pgp_alter_test set birthdate = pgp_sym_encrypt(birthdate, 'AES_KEY') where id = 1; UPDATE 1 test=# select * from pgp_alter_test ; id | birthdate ----+------------------------------------------------------------------------------------------------------------------------------------------------------------ 1 | \xc30d04070302b3f55c80f9ab657c68d23b010cd9d4d82631f89c786394a8bceb35529db07c708e5a0c4f04cf91aed24b5ff397dd99c678ec4f5bb769e148cfae3cdfc5453daaeb34ddd2737c (1 row) ^ test=# select * from pgp_alter_test where pgp_sym_decrypt(birthdate::bytea, 'AES_KEY') = '2018-06-21'; id | birthdate ----+------------------------------------------------------------------------------------------------------------------------------------------------------------ 1 | \xc30d04070302b3f55c80f9ab657c68d23b010cd9d4d82631f89c786394a8bceb35529db07c708e5a0c4f04cf91aed24b5ff397dd99c678ec4f5bb769e148cfae3cdfc5453daaeb34ddd2737c (1 row) I am at a loss now. The only thing I can think of is that data itself is actually corrupted. Maybe some sort of language encoding/collation issue. Just not sure how to test that at the moment. > > I'm just afraid it can happen in production.... > >> >> create table pgp_test(id integer, fld_1 varchar); >> >> insert  into pgp_test values (1, pgp_sym_encrypt('2018-06-21', >> 'AES_KEY')) >> >> 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')? > Yes, it seems to have the same value So > > > -- Adrian Klaver adrian.klaver@aklaver.com