Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.92) (envelope-from ) id 1jCpo2-0001p0-7Q for pgsql-general@arkaria.postgresql.org; Fri, 13 Mar 2020 19:17:14 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1jCpo0-0000Y9-Eg for pgsql-general@arkaria.postgresql.org; Fri, 13 Mar 2020 19:17:12 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1jCpny-0000Wz-Jd for pgsql-general@lists.postgresql.org; Fri, 13 Mar 2020 19:17:12 +0000 Received: from out2-smtp.messagingengine.com ([66.111.4.26]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jCpnt-0003kQ-4H for pgsql-general@lists.postgresql.org; Fri, 13 Mar 2020 19:17:09 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id DBD47223E6; Fri, 13 Mar 2020 15:17:03 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Fri, 13 Mar 2020 15:17:03 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm2; bh=0 mxxFNCE9SbOFZBGcdlbkU4qcJvk9FS3Or0N1YVP7ts=; b=JHRS+4FK1+XlAEJV+ irGyVRQ0wUw1XZrQruc97sr4rW2HjOvi+cvSrI62TtcKjpaebQ0wSYj35lJOxI4A 3wbXXaeJgkZsOg80fgPhZoy1RWX7WOMLAMm7/80/WnesppielE3q5z+seYSbpBfq yTzrEJKVpQpyefPidCeMVfin5dlobiRn4EGRmqaJDKv3AQbK+Dk58iP4F4+8ULnE Oa/QOhkSfyjlZyMtrJZSohhtKtQVesW1Gu84ZS8Lhv2zn3BD/puegNlvGaMs3Aga Ni1cNZaYghkDebq5I6RXqRxlZGc7h5WYems7HrFV67aekZuIBHdXh9vZUMO98aFo ahXEw== 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-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm2; bh=0mxxFNCE9SbOFZBGcdlbkU4qcJvk9FS3Or0N1YVP7 ts=; b=ldy8DoIZfaSLIVqfSARmw5nJlslxKio1mWyfg3MCmvza9xD/UshgpM4jZ WtxT46BXsIs/VE0d6/hTGicw1hfG45xjs3DeeCbrAuZf26GyDjU3Gn60og4XTqlT JDrPmc+CfaVBlGTKg6LvFJIJrsbjhGK7QFOvOpmAzHRLLsboaIBdA8/Z8CjdoLLK xnmXsXafFqqbypQYEqU4Ld2is7jUOszeRL2AOJ9btDez4a/kO8DYWXUeaghB7b0R n3n6S0uVq7xb1uOrENizgJdRgQ9baH7XbR1Zq11LCJgVf9Vb0UcAWx01FYd6tg8m +O6Tf3fbZGttS5lP0GWCf9mrccgXA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedugedruddvjedguddvfecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecunecujfgurhepuffvfhfhkffffgggjggtgfesth ekredttdefjeenucfhrhhomheptegurhhirghnucfmlhgrvhgvrhcuoegrughrihgrnhdr khhlrghvvghrsegrkhhlrghvvghrrdgtohhmqeenucffohhmrghinhepghhithhhuhgsrd gtohhmnecukfhppeejuddrvdduvddruddvledrudelfeenucevlhhushhtvghrufhiiigv pedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgrnhdrkhhlrghvvghrsegrkh hlrghvvghrrdgtohhm X-ME-Proxy: Received: from [192.168.1.10] (unknown [71.212.129.193]) by mail.messagingengine.com (Postfix) with ESMTPA id 32CE0328006E; Fri, 13 Mar 2020 15:17:03 -0400 (EDT) Subject: Re: encrypt/decrypt between javascript and postgresql. To: AC Gomez , pgsql-general@lists.postgresql.org References: From: Adrian Klaver Message-ID: <309850a0-ccd6-8ddb-f6ba-e7999ce73681@aklaver.com> Date: Fri, 13 Mar 2020 12:17:02 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.5.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; 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 3/11/20 5:46 PM, AC Gomez wrote: > I'm trying to encrypt/decrypt between javascript and postgresql. > > I'm using this: > https://gist.github.com/vlucas/2bd40f62d20c1d49237a109d491974eb algorithm to > encrypt my text, and then in PostgreSQL I use PGCRYPTO.decrypt_iv to > decrypt the text. > > I pass in 'ThisISMySign' to the Encrypt function. > > Encrypted string returned from > above: "fc9a03cbc8a57d4061570575f197c29c:a319a4bf354516f392ba96a895478af6" A quick walk through the JS code found: ... let decipher = crypto.createDecipheriv('aes-256-cbc', Buffer.from(ENCRYPTION_KEY), iv); let decrypted = decipher.update(encryptedText); decrypted = Buffer.concat([decrypted, decipher.final()]); return decrypted.toString(); where const ENCRYPTION_KEY = process.env.ENCRYPTION_KEY; Pretty sure the below does not have access to the above. > > I have to remove the colon to get something out...and so this: > > select > decrypt_iv(decode('fc9a03cbc8a57d4061570575f197c29ca319a4bf354516f392ba96a895478af6','hex')::bytea, > 'sKCx49VgtHZ59bJOTLcU0Gr06ogUnDJi'::bytea, 'null'::bytea, > 'aes-cbc/pad:pkcs'); > > Gives me this: 6 á¶ðÒÿÆÛÏBSïÅThisISMySign In my instance(12.1) I get: decrypt_iv ------------------------------------------------------------ \x36df9ec98ff4ad80b9a4b0425390baed5468697349534d795369676e > > "ThisISMySign" was the original string. So I'm getting the right result > in half of the decrypted string. > > The paremeter after the key, 3rd parameter, it can be any string. That > just changes the first part of the output, the garbage part. > > In decrypt_iv I tried using the encryption algorithm name in the > javascript used to encrypt, but that gets me nowhere. > > I cannot see what i'm missing here. > > Thanks -- Adrian Klaver adrian.klaver@aklaver.com