如何判断 python-gnupg 使用了什么密码?

信息安全 加密 pgp gnupg Python 打开pgp
2021-09-03 01:54:05

我正在使用这样的python-gnupg模块加密文件最终,我想使用AES256密码进行加密:

status = gpg.encrypt_file(fin, recipients=['root@frarakee'], output='foo.bin.gpg')

当我在 CLI 上检查文件时,它告诉我这是一条PGP 消息

# file foo.bin.gpg 
foo.bin.gpg: PGP message

如果我gpg在 CLI (Ubuntu 14.04) 上使用加密文件,该file命令会告诉我它的数据gpg显示它正在使用AES256

# gpg --verbose -r root@frarakee --cipher-algo AES256 -e foo.bin
gpg: using PGP trust model
gpg: using subkey 1C32653F instead of primary key A7173E08
gpg: This key belongs to us
gpg: reading from `foo.bin'
File `foo.bin.gpg' exists. Overwrite? (y/N) y
gpg: writing to `foo.bin.gpg'
gpg: RSA/AES256 encrypted for: "132653F root@frarakee <root@frarakee>"

# file foo.bin
foo.bin: data

值得注意的是,python 文档状态:

如果未指定,默认的 cipher_algo 为“AES256”

但是file当我使用symmetric加密时,命令似乎只同意这一点

# gpg --cipher-algo AES256 -c /tmp/foo.bin
# file /tmp/foo.bin.gpg 
foo.bin.gpg: GPG symmetrically encrypted data (AES256 cipher)

我不确定这是file魔法、python-gpg模块中的错误,还是我的理解。我怎样才能知道实际上正在使用的密码通过gpgpython-gpg模块?有更好的地方问这个吗?

2个回答

file仅查找一些魔术字节来确定文件类型,但不完全解析 OpenPGP 数据包。可以通过运行gpg --list-packets查看所有加密详情pgpdump(输出更容易阅读,但需要单独安装)。通过阅读RFC 4880,OpenPGP的可能有助于在理解非常技术性的输出。

TL;博士

gpg -vvv --list-packets大概可以回答大部分问题。

这篇文章建立在 Jens Erat 的回答之上

gpg --list-packets可以使用详细程度和调试开关。请注意,虽然手册页只讨论了最多传递 2 个详细开关,但 gpg 确实响应了最多 3 个详细开关。

--debug-level 1,开关是不是很有意思,--debug-all很有趣,但对上帝的爱做它一个空文件。我的加密空文件产生了 414 行输出,我的 1GB 文件产生了 270 万行。

我已经包含了 pgpdump 的输出示例,以及具有不同详细程度的 gpg。我不得不剪掉--debug-all,因为它占用了太多空间。

仅供参考,我正在运行 gpg (GnuPG) 2.2.27

pgpdump ./big_buck_bunny_1080p_surround.avi.gpg  2>&1  | head -n 15

Old: Public-Key Encrypted Session Key Packet(tag 1)(94 bytes)
        New version(3)
        Key ID - 0xD2E1F0CBF16B1266
        Pub alg - Reserved for Elliptic Curve(pub 18)
                unknown(pub 18)
                -> m = sym alg(1 byte) + checksum(2 bytes) + PKCS-1 block type 02
New: Symmetrically Encrypted and MDC Packet(tag 18)(8192 bytes) partial start
        Ver 1
        Encrypted data [sym alg is specified in pub-key encrypted session key]
                (plain text + MDC SHA1(20 bytes))
New:    (8192 bytes) partial continue
New:    (8192 bytes) partial continue
New:    (8192 bytes) partial continue
New:    (8192 bytes) partial continue
New:    (8192 bytes) partial continue
$ gpg --list-packets ./big_buck_bunny_1080p_surround.avi.gpg

gpg: encrypted with 255-bit ECDH key, ID D2E1F0CBF16B1266, created 2021-03-23
      "your dog (doggy) <dog@dog.dog>"
# off=0 ctb=84 tag=1 hlen=2 plen=94
:pubkey enc packet: version 3, algo 18, keyid D2E1F0CBF16B1266
        data: [263 bits]
        data: [392 bits]
# off=96 ctb=d2 tag=18 hlen=2 plen=0 partial new-ctb
:encrypted data packet:
        length: unknown
        mdc_method: 2
# off=117 ctb=a3 tag=8 hlen=1 plen=0 indeterminate
:compressed packet: algo=2
# off=119 ctb=ae tag=11 hlen=5 plen=928670793
:literal data packet:
        mode b (62), created 1616485594, name="big_buck_bunny_1080p_surround.avi",
        raw data: 928670754 bytes
$ gpg -vvv --list-packets ./big_buck_bunny_1080p_surround.avi.gpg 

gpg: using character set 'utf-8'
# off=0 ctb=84 tag=1 hlen=2 plen=94
:pubkey enc packet: version 3, algo 18, keyid D2E1F0CBF16B1266
        data: [263 bits]
        data: [392 bits]
gpg: public key is D2E1F0CBF16B1266
gpg: using subkey D2E1F0CBF16B1266 instead of primary key CCD3793A53651BF6
gpg: public key encrypted data: good DEK
# off=96 ctb=d2 tag=18 hlen=2 plen=0 partial new-ctb
:encrypted data packet:
        length: unknown
        mdc_method: 2
gpg: using subkey D2E1F0CBF16B1266 instead of primary key CCD3793A53651BF6
gpg: encrypted with 255-bit ECDH key, ID D2E1F0CBF16B1266, created 2021-03-23
      "your dog (doggy) <dog@dog.dog>"
gpg: AES256 encrypted data
# off=117 ctb=a3 tag=8 hlen=1 plen=0 indeterminate
:compressed packet: algo=2
# off=119 ctb=ae tag=11 hlen=5 plen=928670793
:literal data packet:
        mode b (62), created 1616485594, name="big_buck_bunny_1080p_surround.avi",
        raw data: 928670754 bytes
gpg: decryption okay