python gnupg.encrypt:没有错误,但不加密数据或文件 [英] python gnupg.encrypt : no errors but not encrypting data or files

查看:398
本文介绍了python gnupg.encrypt:没有错误,但不加密数据或文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows 7上使用python-gnupg v0.3.5 w / Python 2.7和GPG4Win v2.2.0



test_gnupg.py导致2个失败:



测试搜索键是否正常... FAIL



Doctest :gnupg.GPG.recv_keys ...失败



2个键盘存在于每个这些位置(在GPGHome目录下(C:\Program Files(x86))中



)\GNU\GnuPG)



在用户配置文件(C:\Users\\\AppData\Roaming\gnupg)下



如果我创建GPG实例并将keyring文件路径设置为用户配置文件pubring.pgp,我得到GPG的结果.list_keys()。如果我让它使用gpghome目录pubring.pgp我没有从list_keys()的结果,因为keyring是空的。



所以给定我指定用户配置文件keyring和我有一个关键使用这是发生了什么:

 >>> data ='1234 abcd 56678'
>>>>><指纹>'
>>> enc = gpg.encrypt(data,fingerprint)
>>> enc.data
''

encrypt_file()给出相同的结果,没有任何反应,没有错误。我不是特别精明的,但似乎如果我有数据和公钥这应该是死的简单。我有一个可怕的时间试图确定什么是错误,因为我看不到日志文件在任何地方,我没有错误,尝试这个。



我如何确定什么是在这里出错?
我已经阅读了很多我可以在StackOverflow上找到的所有内容, http:// pythonhosted.org/python-gnupg/#getting-started 和python-gnupg的google组。



另外为什么我有两个单独的键盘串在第一位?



编辑:
澄清有两个单独的pubring和secring套件



编辑2:
以下答案有助于导致实际问题。
gnupg.GPG()构造函数正在设置gpg命令行选项,包括'no-tty',调用gnupg.GPG(options ='')解决问题并成功加密数据和文件。

解决方案

好的,我终于找到了,并从命令行获得了基本的加密功能。这里有一个例子,可以加密从命令行输入的数据:

  import gnupg 

gpg_home =/ path / to / gnupg / home
gpg = gnupg.GPG(gnupghome = gpg_home)

data = raw_input(输入要加密的数据:)
rkey = raw_input(输入收件人的密钥ID:)

encrypted_ascii_data = gpg.encrypt(data,rkey)

打印(encrypted_ascii_data)

将gpg_home更改为要使用的两个GnuPG路径。第一个看起来像默认的安装位置,第二个显示为特定于您的用户帐户。该脚本将提示一些文本加密和加密的密钥ID,然后将ASCII加密加密数据打印到stdout。



编辑:我不确定,但我怀疑您的代码失败的原因是由于使用整个指纹的收件人密钥ID,这是不必要的(我使用0xLONG格式,一个例子在我的配置文件),或者你打错了GPG主目录。



编辑2:这可以加密文件并将输出写入同一个目录中的文件,它将像在* nix系统上一样工作。你需要像上面的例子一样更改gpg_home:

  import gnupg 

gpg_home = 〜/ .gnupg
gpg = gnupg.GPG(gnupghome = gpg_home)

data = raw_input(输入要加密的文件的完整路径)
rkeys = raw_input 输入用空格分隔的键ID:)
savefile = data +。asc

afile = open(data,rb)
encrypted_ascii_data = gpg.encrypt_file ,rkeys.split(),always_trust = True,output = savefile)
afile.close()



我的工作完成了! :)



BTW,这两个示例都使用Python 2.7,对于Python 3,您需要修改raw_input()行以使用input()。 >

using python-gnupg v0.3.5 on windows 7 w/Python 2.7 and GPG4Win v2.2.0

test_gnupg.py results in 2 failures:

Test that searching for keys works ... FAIL

Doctest: gnupg.GPG.recv_keys ... FAIL

2 keyrings exist in each of these locations(secring & pubring in each):

under the GPGHome directory (C:\Program Files (x86)\GNU\GnuPG)

under the user profile(C:\Users\\AppData\Roaming\gnupg)

If I create GPG instance and set the keyring file path to the user profile pubring.pgp I get a result from GPG.list_keys(). If I let it use the gpghome directory pubring.pgp I get no results from list_keys() because that keyring is empty.

So given I specify the user profile keyring and I have a key to use this is what happens:

    >>>data = '1234 abcd 56678'
    >>>fingerprint = u'<fingerprint>'
    >>>enc = gpg.encrypt(data,fingerprint)
    >>>enc.data
    ''

encrypt_file() gives the same results, nothing happens, no errors. I'm not particularly savvy in any of this but it seems like if I have data and public key this should be dead simple. I'm having a horrendous time trying to determine what is wrong given I see no log files anywhere and I have no errors when attempting this.

How can I determine what is going wrong here? I've read pretty much everything I can find here on StackOverflow, http://pythonhosted.org/python-gnupg/#getting-started and the google group for python-gnupg.

Also why do I have 2 separate sets of keyrings in the first place?

edit: clarified there are 2 separate sets of pubring and secring

edit 2: answer below was instrumental in leading to the actual problem. the gnupg.GPG() constructor is setting gpg command line options that include 'no-tty', calling gnupg.GPG(options='') resolves the issue and successfully encrypts both data and files.

解决方案

Okay, I finally got around to looking at this and got basic encryption to work from the command line. Here's an example that will work to encrypt data entered from the command line:

import gnupg

gpg_home = "/path/to/gnupg/home"
gpg = gnupg.GPG(gnupghome=gpg_home)

data = raw_input("Enter data to encrypt: ")
rkey = raw_input("Enter recipient's key ID: ")

encrypted_ascii_data = gpg.encrypt(data, rkey)

print(encrypted_ascii_data)

Change the gpg_home to whichever of those two GnuPG paths you want to use. The first one looks like the default installation location and the second one appears to be specific to your user account. The script will prompt for some text to encrypt and a key ID to encrypt to, then print the ASCII armoured encrypted data to stdout.

EDIT: I'm not certain, but I suspect the reason your code failed was either due to using the whole fingerprint for the recipient key ID, which is unnecessary (I used the 0xLONG format, an example of which is on my profile), or you called the wrong GPG home directory.

EDIT 2: This works to encrypt files and writes the output to a file in the same directory, it will work as is on *nix systems. You will need to change the gpg_home as with the above example:

import gnupg

gpg_home = "~/.gnupg"
gpg = gnupg.GPG(gnupghome=gpg_home)

data = raw_input("Enter full path of file to encrypt: ")
rkeys = raw_input("Enter key IDs separated by spaces: ")
savefile = data+".asc"

afile = open(data, "rb")
encrypted_ascii_data = gpg.encrypt_file(afile, rkeys.split(), always_trust=True, output=savefile)
afile.close()

My work here is done! :)

BTW, both these examples use Python 2.7, for Python 3 you'll need to modify the raw_input() lines to use input() instead.

这篇关于python gnupg.encrypt:没有错误,但不加密数据或文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆