【问题标题】:python gnupg decryption failing due to bad password on generated keys由于生成的密钥上的密码错误,python gnupg 解密失败
【发布时间】:2019-04-08 09:08:41
【问题描述】:

我正在尝试使用来自here 的 Python gnupg 包进行 GPG 加密。我编写了一些示例代码以确保我正确使用了 API,但大多数现有的包示例都使用主目录。我希望能够导入/导出密钥并通过它与 API 交互。

我的测试代码如下:

def doEncryptFile(pubKeyFile, inDataFile):
    f = open(pubKeyFile,"r")
    data = f.read()
    f.close()
    gpg = gnupg.GPG()
    import_result = gpg.import_keys(data)
    public_key = gpg.list_keys()[0]

    f = open(inDataFile,"r")
    decData = f.read()
    f.close()

    encrypted = gpg.encrypt(decData, public_key['fingerprint'])
    print("encrypted?")
    print(str(encrypted.ok))
    print(str(encrypted.status))
    print(str(encrypted))

    return str(encrypted)

def doDecryptFile(privKeyFile, inDataFile, privPass):
    f = open(privKeyFile,"r")
    data = f.read()
    f.close()
    gpg = gnupg.GPG()
    import_result = gpg.import_keys(data)
    public_key = gpg.list_keys()[0]

    f = open(inDataFile,"rb")
    decData = f.read()
    f.close()

    decrypted_data = gpg.decrypt(decData, passphrase=privPass)
    print("decrypted?")
    print(str(decrypted_data.ok))
    print(str(decrypted_data.status))


gpg = gnupg.GPG()
key = do_key_generation(gpg, "helloWorld")
print(str(type(key)))
private_key = gpg.export_keys(key.fingerprint, True, passphrase="helloWorld")
public_key = gpg.export_keys(key.fingerprint)

with open('sample_public.asc', 'w') as f:
    f.write(public_key)

with open('sample_private.asc', 'w') as f:
    f.write(private_key)


doEncryptFile(r"sample_public.asc", "sampleDecryptedData.txt")
doDecryptFile(r"sample_private.asc", "sampleEncrypted.txt", privPass="helloWorld")

在上面的例子中,我手动将密文复制到sampleEncrypted.txt.,密钥生成函数取自here。以这种方式使用它时,加密按预期工作,我得到了 ASCII 编码的 blob。

但是,当尝试解密文件时,解密失败。如果我不提供密码,我会收到来自 OpenPGP 的提示,告诉我输入密码,所以它至少可以部分工作,但解密失败并且状态消息只是“解密失败”。如果我尝试在 pinentry-qt GUI 中手动输入“helloWorld”密码,则错误消息为“Bad Passphrase”。我也尝试过将decrypt_file与包含ASCII blob的输入文件一起使用,如python-gnupg页面所述,得到相同的结果。

如果有什么不同的话,我在 Windows 系统上使用 Python 3。我还要注意,当通过命令行使用 gpg 时,一切都按预期工作。

【问题讨论】:

  • 最小的,完整的。您也应该发布 key_gen 步骤
  • doEncryptFile 的结果怎么办???

标签: python python-3.x encryption gnupg


【解决方案1】:

您忘记将输出保存到文件中。

我将output= 选项添加到gpg.encryptgpg.decrypt,当然还有您的函数。

import gnupg

def do_key_generation(gpg, passphrase = "helloWorld"):

    input_data = gpg.gen_key_input(
        name_email='me@email.com',
        passphrase=passphrase,
    )
    key = gpg.gen_key(input_data)
    print(key)
    return key

def doEncryptFile(pubKeyFile, inDataFile, outputDatafile):
    f = open(pubKeyFile,"r")
    data = f.read()
    f.close()
    gpg = gnupg.GPG()
    import_result = gpg.import_keys(data)
    public_key = gpg.list_keys()[0]

    f = open(inDataFile,"rb")
    decData = f.read()
    f.close()


    encrypted = gpg.encrypt(decData, public_key['fingerprint'],output=outputDatafile)
    print("encrypted?")
    print(str(encrypted.ok))
    print(str(encrypted.status))
    print(str(encrypted))

def doDecryptFile(privKeyFile, inDataFile, privPass,outputDatafile):
    f = open(privKeyFile,"r")
    data = f.read()
    f.close()
    gpg = gnupg.GPG()
    import_result = gpg.import_keys(data)
    public_key = gpg.list_keys()[0]

    f = open(inDataFile,"rb")
    decData = f.read()
    f.close()

    decrypted_data = gpg.decrypt(decData, passphrase=privPass,output=outputDatafile)
    print("decrypted?")
    print(str(decrypted_data.ok))
    print(str(decrypted_data.status))


gpg = gnupg.GPG()
key = do_key_generation(gpg, "helloWorld")

print(str(type(key)))

private_key = gpg.export_keys(key.fingerprint, True, passphrase='helloWorld')
public_key = gpg.export_keys(key.fingerprint)

with open('sample_public.asc', 'w') as f:
    f.write(public_key)

with open('sample_private.asc', 'w') as f:
    f.write(private_key)


doEncryptFile(r"sample_public.asc", "sampleFile.txt","sampleEncrypted.txt")
doDecryptFile(r"sample_private.asc", "sampleEncrypted.txt", privPass="helloWorld", outputDatafile="sampleDecrypted.txt" )

【讨论】:

  • 那么这是否意味着输出只能转到一个文件?这似乎不太理想。
  • @Fewmitz 实际上是双向的。您想保存到文件中,所以我使用了这种方式。请参阅source code
猜你喜欢
  • 1970-01-01
  • 2018-11-28
  • 1970-01-01
  • 2015-04-03
  • 1970-01-01
  • 2021-01-22
  • 2013-01-29
  • 1970-01-01
  • 2016-12-25
相关资源
最近更新 更多