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 1fWLxB-0002I3-Kc for pgsql-general@arkaria.postgresql.org; Fri, 22 Jun 2018 13:18:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fWLx7-0003O1-Mz for pgsql-general@arkaria.postgresql.org; Fri, 22 Jun 2018 13:18:13 +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 1fWLx7-0003Nu-Bs for pgsql-general@lists.postgresql.org; Fri, 22 Jun 2018 13:18:13 +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 1fWLx4-000699-L4 for pgsql-general@lists.postgresql.org; Fri, 22 Jun 2018 13:18:12 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 006EA21B3F; Fri, 22 Jun 2018 09:18:09 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Fri, 22 Jun 2018 09:18:10 -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=7hOvP1MB69JDn0huoLgeQcBXznywE 6mK/crv1qmSGdA=; b=gFBG88vjnTW0hbd5KyzKyBRViBq34PdzV5Gkk0Bd7feVn eV4EoD5nUrrpW1WpN52JYF2YyMo9J1gBxw/jxJzVbqouwkAFVavIJct+A2IZtgv+ 2/CPsaRBHa/g4YptfA3AEn6cmkipEssc/Bgt5DXK38uocgbKUktyZ7ZBJ8Ept9Km sdV0FXPwkwdWJ2pqwFxuIc+k3gkjYlNHxthHirC9+UrjlEznSZk7FgNPX+/pIEtg JbiD90SLPFAtYYMHpE6UPVt1kMZQMj+piXrKK75Q8iFgAaNXdspU4wJZTGHn7fLF amHyfz/pnVXmYCWq3eXGRKU3RIlZROLMxfZyvkU1w== 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=7hOvP1 MB69JDn0huoLgeQcBXznywE6mK/crv1qmSGdA=; b=rgocL7yePt75J3rkxgV4Sw Tbzyg8+3f+xRgtfmc5s9juxxvKso1FUahB4AlKPI1Bhh2KTNLI0MHNRqs8kH1fMl TqvsSgQmlzKhpxC384qizEGKXtVu6zr6VbTg7sEL/nCsHdsyDO+VdL7hPO1+G0z+ gRPOEhLy71b3/3Vy+WlrWW/NgipN1YeFKaMmcWGZjaIT6nOqRtJvSurr9M7iDFW0 qt4kfq16YX1ifjMyuZBRry8jcWidyLaH/RAhKdTaY75JAil94hxQWcFSn5UxDeiQ ks2WXq68fa/ufb4qvyf+PE8gX5Lf8KvIrgwLa9xHl8WAeEn3Y0hfIFwnA0QLEWEA == 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 53ADDE44D5; Fri, 22 Jun 2018 09:18:09 -0400 (EDT) Subject: Re: pgp_sym_decrypt() - error 39000: wrong key or corrupt data To: Moreno Andreo , pgsql-general@lists.postgresql.org References: <1d7de609-5738-bf51-f0b9-b32a889f04e1@evolu-s.it> <94ecaa31-5960-6264-81f9-c8560a3f9fed@aklaver.com> From: Adrian Klaver Message-ID: Date: Fri, 22 Jun 2018 06:18:08 -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 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