【发布时间】:2021-11-30 01:40:10
【问题描述】:
我确实在 Debian linux 系统上使用 gpg 生成了一个私钥/公钥对。 命令行中的加密和解密运行良好。 然后我确实尝试使用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
并且私钥在 private-keys-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 不再知道密钥。
- 如何让我的密钥恢复工作,以便再次解密?
- 如何让我的 python 代码运行?
【问题讨论】:
标签: python python-3.x linux encryption gnupg