我在Debian系统上生成了一个带有gpg的privat/public键区。命令行中的加密和解密工作良好。然后,我尝试用python程序进行解密:
import gnupg
gpg = gnupg.GPG(homedir='/usr/home/username/.gnupg')
encrypted_string = '''-----BEGIN PGP MESSAGE-----
...encrypted data...
-----END PGP MESSAGE-----'''
decrypted_data = gpg.decrypt(encrypted_string, passphrase='mypassphrase')
print('ok: ', decrypted_data.ok)
print('status: ', decrypted_data.status)
print('decrypted string: ', decrypted_data.data)在我运行python代码一次(解密失败,缺少消息秘密密钥)之后,命令行中的解密(与前面相同的命令)也会对消息失败:
gpg: decryption failed: No secret key检查gpg版本
gpg --version返回
gpg (GnuPG) 2.2.12
libgcrypt 1.8.4
...
Home: /usr/home/username/.gnupg
...在这个主文件夹中,所有文件都已就位:
4 drwx------ 2 username username 4096 Jun 28 09:26 openpgp-revocs.d
4 drwx------ 2 username username 4096 Jun 28 09:26 private-keys-v1.d
4 -rw------- 1 username username 32 Oct 11 00:25 pubring.gpg
4 -rw-r--r-- 1 username username 2480 Jun 28 09:26 pubring.kbx
4 -rw------- 1 username username 32 Jun 28 09:23 pubring.kbx~
0 srwx------ 1 username username 0 Jun 29 16:19 S.gpg-agent
0 srwx------ 1 username username 0 Jun 29 16:19 S.gpg-agent.browser
0 srwx------ 1 username username 0 Jun 29 16:19 S.gpg-agent.extra
0 srwx------ 1 username username 0 Jun 29 16:19 S.gpg-agent.ssh
4 -rw------- 1 username username 1280 Oct 11 01:39 trustdb.gpg私钥在私钥-v1.d文件夹中。无论如何,gpg --list-keys和gpg --list-secret-keys都没有显示任何结果。使用
gpg --list-keys --debug-level 9 我得到了以下输出:
gpg: enabled debug flags: packet mpi crypto filter iobuf memory cache memstat trust ipc clock lookup extprog
gpg: DBG: [not enabled in the source] start
gpg: DBG: [not enabled in the source] keydb_new
gpg: DBG: [not enabled in the source] keydb_search_reset
gpg: DBG: keydb_search: reset (hd=0x00008b9427390f20)
gpg: DBG: [not enabled in the source] keydb_search enter
gpg: DBG: keydb_search: 1 search descriptions:
gpg: DBG: keydb_search 0: FIRST
gpg: DBG: keydb_search: searching keybox (resource 0 of 1)
gpg: DBG: keydb_search: searched keybox (resource 0 of 1) => EOF
gpg: DBG: [not enabled in the source] keydb_search leave (not found)
gpg: DBG: [not enabled in the source] stop
gpg: keydb: handles=1 locks=0 parse=0 get=0
gpg: build=0 update=0 insert=0 delete=0
gpg: reset=1 found=0 not=1 cache=0 not=0
gpg: kid_not_found_cache: count=0 peak=0 flushes=0
gpg: sig_cache: total=0 cached=0 good=0 bad=0
gpg: random usage: poolsize=600 mixed=0 polls=0/0 added=0/0
outmix=0 getlvl1=0/0 getlvl2=0/0
gpg: rndjent stat: collector=0x0000000000000000 calls=0 bytes=0
gpg: secmem usage: 0/65536 bytes in 0 blocks我想searched keybox (resource 0 of 1) => EOF是这里的问题,但我不知道为什么。
我试图再次以二进制或文本文件的形式导入密钥文件,但是转到消息中。
gpg: no valid OpenPGP data found.我也试过
gpg --update-trustdb和
gpg --refresh-keys没有任何变化。
我不知道运行python代码时发生了什么,我也不知道为什么gpg不再知道密钥了。
发布于 2021-10-18 21:46:25
在此期间,我找出了根本原因:
python命令
decrypted_data = gpg.decrypt(encrypted_string, passphrase='mypassphrase')忽略pubring.kbx并生成文件pubring.gpg。对于(或多或少为空的) pubring.gpg,命令行中的解密使用pubring.gpg而不是pubring.kbx。正因为如此,解密不再有效。简单地删除了pubring.gpg,命令行中的解密又开始工作了.
答案不仅在代码中。在运行代码之前,我所做的就是安装gnupg。
pip install gnupg和后来的python-gnupg
pip install python-gnupg但是,gnupg和python都有包含在
import gnupg至少在我的maschine上,它总是使用第一次安装的gnupg,而不是python。当我明白这一点时,解决方案就很简单了。我卸载了gnupg
pip uninstall gnupg由于python中的gnupg使用的参数不多,所以我不得不修改home参数:
gpg = gnupg.GPG(gnupghome='/usr/home/username/.gnupg')然后用python程序运行完美的下降。
无论如何,还有一个问题:
如何在python中获得错误或警告消息,因为在这种情况下,安装了两个(或更多)包的类名相同?
https://stackoverflow.com/questions/69523922
复制相似问题