使用 python gnupg,我的私钥被读入为 public 密钥
Using python gnupg, my private key is being read in as a public key
我不确定是什么导致了这个问题。我正在使用 Python 3.6.1 :: Anaconda 4.4.0(64 位)和 gnupg 版本 2.3.1.
我的代码如下:
import gnupg
gpg = gnupg.GPG(binary='C:/GnuPG/bin/gpg.exe', homedir='./pgp')
pri_key = 'pri_key.pgp'
pub_key = 'pub_key.pgp'
key_data = open(pri_key, 'rb').read()
import_result = gpg.import_keys(key_data)
print(import_result.results)
gpg.list_keys(secret=True) # []
gpg.list_keys()
'''
[{'algo': '1',
'date': '1510176716',
'dummy': '',
'expires': '',
'fingerprint': '...',
'keyid': '...',
'length': '2048',
'ownertrust': '-',
'rev': {},
'sigs': {'Alex F <alexf@outlook.com>': []},
'subkeys': [['...', 'e']],
'trust': '-',
'type': 'pub',
'uids': ['Alex F <alexf@outlook.com>']}]
'''
我的私钥如下:
-----BEGIN PGP PRIVATE KEY BLOCK-----
Version: OpenPGP.js v2.0.0
Comment: http://openpgpjs.org
xcLYB...
-----END PGP PRIVATE KEY BLOCK-----
我也能读入 public 键。
我这里也开过casehttps://github.com/isislovecruft/python-gnupg/issues/216.
更新:
感谢@fuglede 提供 GnuPG 1.4.2 版本的提示。我可以从这里 https://lists.gnupg.org/pipermail/gnupg-announce/2015q4/000382.html 下载它。
下载后成功读入私钥。然而,
当我尝试读取我的 public 键时,它似乎没有被加载。这是附有输出的完整代码。
import gnupg
gpg = gnupg.GPG(binary='C:/GnuPG/bin/gpg.exe', homedir='./pgp')
pri_key = 'private_key.pgp'
pub_key = 'public_key.pgp'
'''
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: OpenPGP.js v2.0.0
xsBNBFoDd8wBCADGDhfQRVyBY7ttCRyRwR6wrzmmQir6cPS+w/3ftQbeCyJ7
...
f9VcgxXoj2EOmCVrUu5kOYwsDgZJnz/+yGs1afaAXwr/VPIusHplkdYqWcgO
m0h4GrUKz1NJArQd0lo=
=ZV3T
-----END PGP PUBLIC KEY BLOCK-----
'''
gpg.list_keys()
'''
[]
'''
key_data_pri = open(pri_key, 'rb').read()
import_result = gpg.import_keys(key_data_pri)
print(import_result.results)
'''
[{'fingerprint': '61A...37E', 'status': 'Contains private key\n'}, {'fingerprint': '61A...37E', 'status': 'Entirely new key\n'}]
'''
key_data_pub = open(pub_key, 'rb').read()
import_result = gpg.import_keys(key_data_pub)
print(import_result.results)
'''
[{'fingerprint': '61A...37E', 'status': 'Not actually changed\n'}]
'''
gpg.list_keys(secret=True)
'''
[{'algo': '1',
'date': '1510176716',
'dummy': '',
'expires': '',
'fingerprint': '61A...37E',
'keyid': '...',
'length': '2048',
'ownertrust': '',
'rev': {},
'sigs': {'Alex F <alexf@outlook.com>': []},
'subkeys': [],
'trust': '',
'type': 'sec',
'uids': ['Alex F <alexf@outlook.com>']}]
'''
gpg.list_keys()
'''
[{'algo': '1',
'date': '1510176716',
'dummy': '',
'expires': '',
'fingerprint': '61A...37E',
'keyid': '...',
'length': '2048',
'ownertrust': '-',
'rev': {},
'sigs': {'Alex F <alexf@outlook.com>': []},
'subkeys': [['FBBD...312', 'e']],
'trust': '-',
'type': 'pub',
'uids': ['Alex F <alexf@outlook.com>']}]
'''
unencrypted_string = 'Who are you? How did you get in my house?'
encrypted_data = gpg.encrypt(unencrypted_string,
'alexf@outlook.com',
'alexf@outlook.com',
always_trust=True)
encrypted_string = str(encrypted_data)
print ('ok: ', encrypted_data.ok)
print ('status: ', encrypted_data.status)
print ('stderr: ', encrypted_data.stderr)
print ('unencrypted_string: ', unencrypted_string)
print ('encrypted_string: ', encrypted_string)
'''
ok: False
status: invalid recipient
stderr: gpg: --recipient: skipped: public key not found
[GNUPG:] INV_RECP 1 --recipient
gpg: [stdin]: encryption failed: public key not found
unencrypted_string: Who are you? How did you get in my house?
encrypted_string:
'''
我认为现在的问题是 public 密钥没有被读取,正如 Not actually changed\n
在读取它时所证明的那样。当我尝试加密时,它说 stderr: gpg: --recipient: skipped: public key not found
.
这只是回答的一半,但显然不适合发表评论。在 Python 3.6.3、python-gnupg 2.3.1 和 gpg 1.4.20 以及 Windows:
中,你所做的对我来说都很好
C:\Users\username>gpg --version
gpg (GnuPG) 1.4.20
Copyright (C) 2015 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Home: ~/.gnupg
Supported algorithms:
Pubkey: RSA, RSA-E, RSA-S, ELG-E, DSA
Cipher: IDEA, 3DES, CAST5, BLOWFISH, AES, AES192, AES256, TWOFISH,
CAMELLIA128, CAMELLIA192, CAMELLIA256
Hash: MD5, SHA1, RIPEMD160, SHA256, SHA384, SHA512, SHA224
Compression: Uncompressed, ZIP, ZLIB, BZIP2
C:\Users\username>pip show gnupg
Name: gnupg
Version: 2.3.1
Summary: A Python wrapper for GnuPG
Home-page: https://github.com/isislovecruft/python-gnupg
Author: Isis Agora Lovecruft
Author-email: isis@patternsinthevoid.net
License: GPLv3+
Location: c:\users\username\appdata\local\continuum\anaconda3\lib\site-packages
Requires: psutil
C:\Users\username>ipython
Python 3.6.3 |Anaconda custom (64-bit)| (default, Oct 27 2017, 12:22:41) [MSC v.1900 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 6.1.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import gnupg
In [2]: gpg = gnupg.GPG(binary='C:/Users/username/AppData/Local/Programs/Git/usr/bin/gpg.exe', homedir='./gpg')
In [3]: gpg.list_keys()
Out[3]: []
In [4]: key_data = '-----BEGIN PGP PRIVATE KEY BLOCK-----\nVersion: GnuPG v1\n\nlQPGBFoTDywBCADbf6BS2JOp3KUP7IHBvuS/xX8zPHHET7DZsQc/DluN+nhHUwHm\n+aERadI642O5kWsd
...: EyhKWXjT8pmdZXiC154CqjRbFN7l5Gbs8juYI9VmM++Aw86e\nePcGARGwLW9WMu5z9/5C/5wkHXsOucguV0oLSuAUa5g6odfRoffSbw5FOuRcte6y\n3xG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OWCnCnzglt\nDoe//s7/9xT2snCEGJ9Ebs42N/6IGLEKIpJymU7FcaEb1/f2k9aYPSiZxKHZDidC\nyKAZxPA+YMvsstTH8OqH391v6sJ+yK5cO5dnkA
...: 1/oEhrnKqDvlBT6rbo2GbB4X44\ngsb5u9eBimrpH66QsMQSnHXE1aw5fqajE5t+MWBd\n=MpyL\n-----END PGP PRIVATE KEY BLOCK-----\n'
In [5]: import_result = gpg.import_keys(key_data)
In [6]: import_result.results
Out[6]:
[{'fingerprint': 'D2E8871D9EFFD91B1569FD48105FDBE9E1923472',
'status': 'Contains private key\n'},
{'fingerprint': 'D2E8871D9EFFD91B1569FD48105FDBE9E1923472',
'status': 'Entirely new key\n'}]
In [7]: gpg.list_keys(secret=True)
Out[7]:
[{'algo': '1',
'date': '1511198508',
'dummy': '',
'expires': '',
'fingerprint': 'D2E8871D9EFFD91B1569FD48105FDBE9E1923472',
'keyid': '105FDBE9E1923472',
'length': '2048',
'ownertrust': '',
'rev': {},
'sigs': {'qweqeq (qwe) <qweqwe@qwe.qwe>': []},
'subkeys': [],
'trust': '',
'type': 'sec',
'uids': ['qweqeq (qwe) <qweqwe@qwe.qwe>']}]
您能否提供观察到的行为成立的 key_data
值?
更新问题的更新答案:
用于 GPG.encrypt
的语法已关闭。根据 the documentation,data
的加密是通过 gpg.encrypt(data, recipients)
执行的,其中“recipients
是这些收件人的密钥指纹列表。”在我们上面的示例中,这意味着消息可以按如下方式加密:
In [54]: encrypted_data = gpg.encrypt('test', 'D2E8871D9EFFD91B1569FD48105FDBE9E1923472')
In [55]: encrypted_data.ok, str(encrypted_data)
Out[55]:
(True,
'-----BEGIN PGP MESSAGE-----\n\nhQEMA6vzRkOvQDF8AQgAkykuXaK7sujYU4uZseqXsYpKw7TZdSagAu86jOq+JMyC\nGygnWdcUSevilDE97RE6TJjrYa3Q+fM+Dn9xwtaco2g6W/ZKF9EPIrLuVaC8NpM4\nuKNm5SHE9orHPbZpFw3w1JuC8uZqk/XaNXHz7V7TD2KAbyxZ4qMtvhuSYvaU00Rg\nIrPOc66S8PtnKIcpjVUgz33KKr1Pnoa9dMcrfJfpvKU2iaAflgTW0jF7WlNwk5eE\n7ZkTILbpN1LNqBg4n9B5hs/oZ0IheQvcbh58SwIODARWtUlxLWGIWIZ1+AEhXmUs\nDl0BI98gw46p2NffetdAjhF8LqM7MmJ73+37ZwMhQ9I/AdE6gkSousPHg5dRmU1y\n0CE5kzA8K+qCgschy/t3cRtBPaf9mXBCcodcF9VHIZrP7ACO7JLSl5hi5iEKPtfm\n=P1fg\n-----END PGP MESSAGE-----\n')
我不确定是什么导致了这个问题。我正在使用 Python 3.6.1 :: Anaconda 4.4.0(64 位)和 gnupg 版本 2.3.1.
我的代码如下:
import gnupg
gpg = gnupg.GPG(binary='C:/GnuPG/bin/gpg.exe', homedir='./pgp')
pri_key = 'pri_key.pgp'
pub_key = 'pub_key.pgp'
key_data = open(pri_key, 'rb').read()
import_result = gpg.import_keys(key_data)
print(import_result.results)
gpg.list_keys(secret=True) # []
gpg.list_keys()
'''
[{'algo': '1',
'date': '1510176716',
'dummy': '',
'expires': '',
'fingerprint': '...',
'keyid': '...',
'length': '2048',
'ownertrust': '-',
'rev': {},
'sigs': {'Alex F <alexf@outlook.com>': []},
'subkeys': [['...', 'e']],
'trust': '-',
'type': 'pub',
'uids': ['Alex F <alexf@outlook.com>']}]
'''
我的私钥如下:
-----BEGIN PGP PRIVATE KEY BLOCK-----
Version: OpenPGP.js v2.0.0
Comment: http://openpgpjs.org
xcLYB...
-----END PGP PRIVATE KEY BLOCK-----
我也能读入 public 键。
我这里也开过casehttps://github.com/isislovecruft/python-gnupg/issues/216.
更新:
感谢@fuglede 提供 GnuPG 1.4.2 版本的提示。我可以从这里 https://lists.gnupg.org/pipermail/gnupg-announce/2015q4/000382.html 下载它。
下载后成功读入私钥。然而, 当我尝试读取我的 public 键时,它似乎没有被加载。这是附有输出的完整代码。
import gnupg
gpg = gnupg.GPG(binary='C:/GnuPG/bin/gpg.exe', homedir='./pgp')
pri_key = 'private_key.pgp'
pub_key = 'public_key.pgp'
'''
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: OpenPGP.js v2.0.0
xsBNBFoDd8wBCADGDhfQRVyBY7ttCRyRwR6wrzmmQir6cPS+w/3ftQbeCyJ7
...
f9VcgxXoj2EOmCVrUu5kOYwsDgZJnz/+yGs1afaAXwr/VPIusHplkdYqWcgO
m0h4GrUKz1NJArQd0lo=
=ZV3T
-----END PGP PUBLIC KEY BLOCK-----
'''
gpg.list_keys()
'''
[]
'''
key_data_pri = open(pri_key, 'rb').read()
import_result = gpg.import_keys(key_data_pri)
print(import_result.results)
'''
[{'fingerprint': '61A...37E', 'status': 'Contains private key\n'}, {'fingerprint': '61A...37E', 'status': 'Entirely new key\n'}]
'''
key_data_pub = open(pub_key, 'rb').read()
import_result = gpg.import_keys(key_data_pub)
print(import_result.results)
'''
[{'fingerprint': '61A...37E', 'status': 'Not actually changed\n'}]
'''
gpg.list_keys(secret=True)
'''
[{'algo': '1',
'date': '1510176716',
'dummy': '',
'expires': '',
'fingerprint': '61A...37E',
'keyid': '...',
'length': '2048',
'ownertrust': '',
'rev': {},
'sigs': {'Alex F <alexf@outlook.com>': []},
'subkeys': [],
'trust': '',
'type': 'sec',
'uids': ['Alex F <alexf@outlook.com>']}]
'''
gpg.list_keys()
'''
[{'algo': '1',
'date': '1510176716',
'dummy': '',
'expires': '',
'fingerprint': '61A...37E',
'keyid': '...',
'length': '2048',
'ownertrust': '-',
'rev': {},
'sigs': {'Alex F <alexf@outlook.com>': []},
'subkeys': [['FBBD...312', 'e']],
'trust': '-',
'type': 'pub',
'uids': ['Alex F <alexf@outlook.com>']}]
'''
unencrypted_string = 'Who are you? How did you get in my house?'
encrypted_data = gpg.encrypt(unencrypted_string,
'alexf@outlook.com',
'alexf@outlook.com',
always_trust=True)
encrypted_string = str(encrypted_data)
print ('ok: ', encrypted_data.ok)
print ('status: ', encrypted_data.status)
print ('stderr: ', encrypted_data.stderr)
print ('unencrypted_string: ', unencrypted_string)
print ('encrypted_string: ', encrypted_string)
'''
ok: False
status: invalid recipient
stderr: gpg: --recipient: skipped: public key not found
[GNUPG:] INV_RECP 1 --recipient
gpg: [stdin]: encryption failed: public key not found
unencrypted_string: Who are you? How did you get in my house?
encrypted_string:
'''
我认为现在的问题是 public 密钥没有被读取,正如 Not actually changed\n
在读取它时所证明的那样。当我尝试加密时,它说 stderr: gpg: --recipient: skipped: public key not found
.
这只是回答的一半,但显然不适合发表评论。在 Python 3.6.3、python-gnupg 2.3.1 和 gpg 1.4.20 以及 Windows:
中,你所做的对我来说都很好C:\Users\username>gpg --version
gpg (GnuPG) 1.4.20
Copyright (C) 2015 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Home: ~/.gnupg
Supported algorithms:
Pubkey: RSA, RSA-E, RSA-S, ELG-E, DSA
Cipher: IDEA, 3DES, CAST5, BLOWFISH, AES, AES192, AES256, TWOFISH,
CAMELLIA128, CAMELLIA192, CAMELLIA256
Hash: MD5, SHA1, RIPEMD160, SHA256, SHA384, SHA512, SHA224
Compression: Uncompressed, ZIP, ZLIB, BZIP2
C:\Users\username>pip show gnupg
Name: gnupg
Version: 2.3.1
Summary: A Python wrapper for GnuPG
Home-page: https://github.com/isislovecruft/python-gnupg
Author: Isis Agora Lovecruft
Author-email: isis@patternsinthevoid.net
License: GPLv3+
Location: c:\users\username\appdata\local\continuum\anaconda3\lib\site-packages
Requires: psutil
C:\Users\username>ipython
Python 3.6.3 |Anaconda custom (64-bit)| (default, Oct 27 2017, 12:22:41) [MSC v.1900 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 6.1.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import gnupg
In [2]: gpg = gnupg.GPG(binary='C:/Users/username/AppData/Local/Programs/Git/usr/bin/gpg.exe', homedir='./gpg')
In [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qARj/Isa5OX\n5S+kk0FraUZcPs2mO4cYoYSBwGX4SBszpZaEYYkBHwQYAQIACQUCWhMPLAIb
...: DAAK\nCRAQX9vp4ZI0cu+0CACH0DgG3qmsnq9bxozGeCUtztNbbJQOFgU2emaIzHDB/8Pl\n4qCmzTRHVxEixZ2u59gG/wqaQuBXw+44oLuQUjjOMm1iYlz2APgYBUq7KOpTh6I+\nEarV1HBFDsoAhdQP
...: XuXbSCSEdBEIJbWXNgKpFZiDXXMnRUCdBalXYMOWCnCnzglt\nDoe//s7/9xT2snCEGJ9Ebs42N/6IGLEKIpJymU7FcaEb1/f2k9aYPSiZxKHZDidC\nyKAZxPA+YMvsstTH8OqH391v6sJ+yK5cO5dnkA
...: 1/oEhrnKqDvlBT6rbo2GbB4X44\ngsb5u9eBimrpH66QsMQSnHXE1aw5fqajE5t+MWBd\n=MpyL\n-----END PGP PRIVATE KEY BLOCK-----\n'
In [5]: import_result = gpg.import_keys(key_data)
In [6]: import_result.results
Out[6]:
[{'fingerprint': 'D2E8871D9EFFD91B1569FD48105FDBE9E1923472',
'status': 'Contains private key\n'},
{'fingerprint': 'D2E8871D9EFFD91B1569FD48105FDBE9E1923472',
'status': 'Entirely new key\n'}]
In [7]: gpg.list_keys(secret=True)
Out[7]:
[{'algo': '1',
'date': '1511198508',
'dummy': '',
'expires': '',
'fingerprint': 'D2E8871D9EFFD91B1569FD48105FDBE9E1923472',
'keyid': '105FDBE9E1923472',
'length': '2048',
'ownertrust': '',
'rev': {},
'sigs': {'qweqeq (qwe) <qweqwe@qwe.qwe>': []},
'subkeys': [],
'trust': '',
'type': 'sec',
'uids': ['qweqeq (qwe) <qweqwe@qwe.qwe>']}]
您能否提供观察到的行为成立的 key_data
值?
更新问题的更新答案:
用于 GPG.encrypt
的语法已关闭。根据 the documentation,data
的加密是通过 gpg.encrypt(data, recipients)
执行的,其中“recipients
是这些收件人的密钥指纹列表。”在我们上面的示例中,这意味着消息可以按如下方式加密:
In [54]: encrypted_data = gpg.encrypt('test', 'D2E8871D9EFFD91B1569FD48105FDBE9E1923472')
In [55]: encrypted_data.ok, str(encrypted_data)
Out[55]:
(True,
'-----BEGIN PGP MESSAGE-----\n\nhQEMA6vzRkOvQDF8AQgAkykuXaK7sujYU4uZseqXsYpKw7TZdSagAu86jOq+JMyC\nGygnWdcUSevilDE97RE6TJjrYa3Q+fM+Dn9xwtaco2g6W/ZKF9EPIrLuVaC8NpM4\nuKNm5SHE9orHPbZpFw3w1JuC8uZqk/XaNXHz7V7TD2KAbyxZ4qMtvhuSYvaU00Rg\nIrPOc66S8PtnKIcpjVUgz33KKr1Pnoa9dMcrfJfpvKU2iaAflgTW0jF7WlNwk5eE\n7ZkTILbpN1LNqBg4n9B5hs/oZ0IheQvcbh58SwIODARWtUlxLWGIWIZ1+AEhXmUs\nDl0BI98gw46p2NffetdAjhF8LqM7MmJ73+37ZwMhQ9I/AdE6gkSousPHg5dRmU1y\n0CE5kzA8K+qCgschy/t3cRtBPaf9mXBCcodcF9VHIZrP7ACO7JLSl5hi5iEKPtfm\n=P1fg\n-----END PGP MESSAGE-----\n')