22

I want to encrypt few files using python what is the best way I can use gpg/pgp using any standard/famous python libraries?

Anurag Uniyal
  • 85,954
  • 40
  • 175
  • 219
  • Encryption is a tricky subject. Please add more information about what you're encrypting and why, and how your app needs to manage it. Specifically, you need to talk about how the secrets should be managed -- that'll determine whether you should use public key encryption or just a shared secret. – Allen Sep 18 '08 at 06:06
  • Basically i just want to encrypt some config files so that it is not readable buy other people but my program only. i don't want any foolproof solution here becuase i will any way have to decrypt files in my program or can there be a fool proof way of doing this? – Anurag Uniyal Sep 22 '08 at 08:45

6 Answers6

12

PyCrypto seems to be the best one around.

Swaroop C H
  • 16,902
  • 10
  • 43
  • 50
  • It's comprehensive and the original author AMK is a respected Python developer. – Swaroop C H Sep 18 '08 at 06:06
  • Yes, but its current maintainer does not have any releases out (as checked today) – Daren Thomas Oct 05 '08 at 19:15
  • 5
    Pycrypto is quite incomplete. It lacks for example the padding schemes for asymmetric encryption schemes. Implementing them yourself is tricky and easily leads to insecure results. Much better is to use one of those libaries that are wrappers around well tested libs like openssl, pgp or gpg. – Accipitridae Oct 09 '09 at 08:26
  • 1
    PyCrypto has all major RSA padding schemes starting from v2.5. In addition to that it does not require any external library, which is a plus I think. – SquareRootOfTwentyThree Aug 15 '12 at 22:24
7

Try KeyCzar

Very easy to implement.

6

I use GPGme The main strength of GPGme is that it read and writes files at the OpenPGP standard (RFC 4880) which can be important if you want to interoperate with other PGP programs.

It has a Python interface. Warning: it is a low-level interface, not very Pythonic.

If you read French, see examples.

Here is one, to check a signature:

signed = core.Data(sys.stdin.read())
plain = core.Data()
context = core.Context()

context.op_verify(signed, None, plain)
result = context.op_verify_result()

sign = result.signatures
while sign:
    if sign.status != 0:
        print "BAD signature from:"
    else:
        print "Good signature from:"
    print "  uid:        ", context.get_key(sign.fpr, 0).uids.uid
    print "  timestamp:  ", sign.timestamp
    print "  fingerprint:", sign.fpr
    sign = sign.next
bortzmeyer
  • 34,164
  • 12
  • 67
  • 91
5

I use pyOpenSSL, its a python binding for OpenSSL which has been around for a long time and is very well tested. I did some benchmarks for my application, which is very crypto intensive and it won hands down against pyCrypto. YMMV.

HughE
  • 523
  • 1
  • 4
  • 10
4

See Google's Keyczar project, which provides a nice set of interfaces to PyCrypto's functionality.

Allen
  • 5,034
  • 22
  • 30
0

I like pyDes (http://twhiteman.netfirms.com/des.html). It's not the quickest, but it's pure Python and works very well for small amounts of encrypted data.

elifiner
  • 7,347
  • 8
  • 39
  • 48