分类: Python/Ruby
2017-11-07 20:37:14
转载: http://blog.sina.com.cn/s/blog_71f3890901011ihw.html
参考:
Date: 2011-10-28 | Modified:
2012-04-08 | Tags: python | 1 Comment
is a Python package
for encrypting and decrypting strings or files using . GPG is an open source alternative to . A popular use of GPG and PGP is encrypting email. For
more information, see the python-gnupg
documentation. Another option for encrypting data from Python is .
This installs the Ubuntu GPG package, creates a test user, and installs the Python package, python-gnupg. This was installed on Ubuntu 10.10 Maverick Meerkat.
$ sudo apt-get install gnupg
$ sudo adduser testgpguser
$ sudo su testgpguser
$ cd
$ virtualenv --no-site-packages venv
$ source venv/bin/activate
$ pip install python-gnupg
$ sudo apt-get install rng-tools
import os
import gnupg
os.system('rm -rf /home/testgpguser/gpghome')
gpg = gnupg.GPG(gnupghome='/home/testgpguser/gpghome')
input_data = gpg.gen_key_input( name_email='testgpguser@mydomain.com',
passphrase='my passphrase')
key = gpg.gen_key(input_data)
print key
B0F4CF530036CE8CD1C064F17D32CEE72C015CD5
(venv)testgpguser@mymachine:~$ cat mykeyfile.asc
(venv)testgpguser@mymachine:~$ cat mykeyfile.asc -----BEGIN PGP PUBLIC KEY BLOCK----- Version: GnuPG v1.4.10 (GNU/Linux) mI0ETqrVGAEEAP42Xs1vQv40MxA3/g/Le5B0VatnDYaSvAhiYfaub79HY4mjYcCD FPDo5b54PSzyhlVsz5RL46+RE9NpQ2JdvFofWi7eVzfdmmTtNYEaiUSmzLUq73Vz qu7P1RhOfwuAyW0otnw/Lw54MVjVZblvp3ln1Fcpleb9ZSrY1h61Y8pHABEBAAG0 REF1dG9nZW5lcmF0ZWQgS2V5IChHZW5lcmF0ZWQgYnkgZ251cGcucHkpIDx0ZXN0 Z3BndXNlckBteWRvbWFpbi5jb20+iLgEEwECACIFAk6q1RgCGy8GCwkIBwMCBhUI AgkKCwQWAgMBAh4BAheAAAoJEH0yzucsAVzVBjwD/1KgTx1y3cpuumu1HF0GtQV0 Wn7l9OaSj98CqQ/f2emHD1l9rrjdt9jm1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ct3KbrprtRxdBrUFdFp+5fTmko/fAqkP39np hw9Zfa643bfY5tYO8ErFrpqSrOe78c+zV8B8O5iOKmeZtHL4P6kXPwQTD/L9loM0 bJpbPez6ZWmrXtZN0H7wwlbOhdaoiEMhC3348QR25lj4cVxMIZ6S3L5NpezWzZxM AA== =v9Z7 -----END PGP PRIVATE KEY BLOCK-----
import gnupg gpg = gnupg.GPG(gnupghome='/home/testgpguser/gpghome') open('my-unencrypted.txt', 'w').write('You need to Google Venn diagram.') with open('my-unencrypted.txt', 'rb') as f: status = gpg.encrypt_file( f, recipients=['testgpguser@mydomain.com'], output='my-encrypted.txt.gpg') print 'ok: ', status.ok print 'status: ', status.status print 'stderr: ', status.stderr ok: True status: encryption ok stderr: [GNUPG:] BEGIN_ENCRYPTION 2 9 [GNUPG:] END_ENCRYPTION (venv)testgpguser@mymachine:~$ cat my-encrypted.txt.gpg -----BEGIN PGP MESSAGE----- Version: GnuPG v1.4.10 (GNU/Linux) hIwDfTLO5ywBXNUBBADo7trFZUD6Ir1vPRAJsoQXDiiw32N1m9/PXWCnQqX0nyzW LfluNMfLFQRclNPVEg+o91qhS71apKvagp8DW7SCDE2SdCYk8nAS3bwAg5+GUyDs XY2E6BQ1cLA1eK1V6D15ih6cq0laRzWuFkehH9PQ5Yp4ZZOmCbopw7dufnYPjdJb AVGLpZRq64SuN1BUWIHbO7vqQGFq7qhGQwuegblEMm4vyr6FBW6JA/x4G/PMfImZ 1cH6KBrWGWrLCTiU/FKG9JvOm8mg8NXzd/TVjPs6rHRaKPFln37T7cLUwA== =FSQP -----END PGP MESSAGE-----
import gnupg gpg = gnupg.GPG(gnupghome='/home/testgpguser/gpghome') with open('my-encrypted.txt.gpg', 'rb') as f: status = gpg.decrypt_file(f, passphrase='my passphrase', output='my-decrypted.txt') print 'ok: ', status.ok print 'status: ', status.status print 'stderr: ', status.stderr ok: True status: decryption ok stderr: [GNUPG:] ENC_TO 16E86B012EFB1D82 1 0 [GNUPG:] USERID_HINT 16E86B012EFB1D82 Autogenerated Key (Generated by gnupg.py) [GNUPG:] NEED_PASSPHRASE 16E86B012EFB1D82 16E86B012EFB1D82 1 0 [GNUPG:] GOOD_PASSPHRASE gpg: encrypted with 1024-bit RSA key, ID 2EFB1D82, created 2011-11-02 "Autogenerated Key (Generated by gnupg.py) " [GNUPG:] BEGIN_DECRYPTION [GNUPG:] PLAINTEXT 62 1320546031 [GNUPG:] PLAINTEXT_LENGTH 32 [GNUPG:] DECRYPTION_OKAY [GNUPG:] GOODMDC [GNUPG:] END_DECRYPTION (venv)testgpguser@mymachine:~$ cat my-decrypted.txt You need to Google Venn diagram.