首页 > 解决方案 > 由于生成的密钥上的密码错误,python gnupg 解密失败

问题描述

我正在尝试使用此处的 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.密钥生成函数取自此处。以这种方式使用它时,加密按预期工作,我得到了 ASCII 编码的 blob。

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

如果这有所作为,我在 Windows 系统上使用 Python 3。我还会注意到,当通过命令行使用 gpg 时,一切都按预期工作。

标签: pythonpython-3.xencryptiongnupg

解决方案


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

我将output=选项添加到gpg.encryptand gpg.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" )

推荐阅读