Decrypting AES GCM 256 with pgcrypto (PostgresQL)



我正在尝试用AES GCM 256加密的pgcrypto解密消息。

加密规格:

Encryption algorithm     AES
Key                      [secret of listener] (64-character-long hexadecimal string in configuration)
Key length               256 bits (32 bytes)
Block mode               GCM
Padding                  None
Initialization vector    In HTTP header (X-Initialization-Vector)
Authentication tag       In HTTP header (X-Authentication-Tag)

所以我收到a:

身体
  • <
  • 键/gh>
  • iv_header
  • auth_tag

我试过下面的

with base as (
select
'F8E2F759E528CB69375E51DB2AF9B53734E393' as body,
'000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F' as key,
'3D575574536D450F71AC76D8' as iv_header,
'19FDD068C6F383C173D3A906F7BD1D83' as auth_tag
),
out as (
select
decrypt_iv(
convert_to(concat(decode(body,'hex'),decode(auth_tag,'hex')),'LATIN1'),
decode(key, 'hex'),
decode(iv_header, 'hex'),
'aes/pad:none'
)
from base
)
select * from out

我一直得到错误decrypt_iv error: Data not a multiple of block size,而我期望得到编码消息{"type": "PAYMENT"}

我预计bodyauth_tag的解码和连接有问题,但不知道是什么。

关于我做了什么/为什么做的一些笔记

  • 我将auth_tag连接到body,因为几个来源是这样描述的。
  • 我使用convert_to函数,因为它似乎是连接两个bytea值的唯一方法

我设法用其他语言(即Snowflake或Python)解密此消息

如果有人看到我做错了什么或有一些指导,我非常感激。

pgcrypto只表示支持CBC和ecb。我不是密码学家,但我不认为这两者与GCM是一回事。所以我不认为你可以用pgcrypto来做这个。我不知道为什么它会导致你得到的确切错误,但它也不让我感到惊讶。

如果我真的需要在数据库中这样做,我认为我可以通过使用pl/python3u编写一个函数来完成。

最新更新