简体   繁体   English

推荐的Python加密模块?

[英]Recommended Python cryptographic module?

I've been exploring what cryptographic modules are available to Python, and I've found 3: ezPyCrypt, yawPyCrypt and KeyCzar (which actually supports a few languages, but Python is included amongst them). 我一直在探索Python可用的加密模块,我发现3:ezPyCrypt,yawPyCrypt和KeyCzar(它实际上支持几种语言,但Python包含在其中)。 The first two rely on the PyCrypto module. 前两个依赖于PyCrypto模块。

Are there choices I am missing? 我缺少选择吗? Is there a clear front-runner for ease and features or does it simply come down to a manner of one's comfort level? 是否有一个明确的领跑者的轻松和功能,还是只是降低到一个舒适的方式?

I'm currently leaning towards KeyCzar, with ezPyCrypt close behind. 我目前倾向于KeyCzar,ezPyCrypt紧随其后。

I would be using the library for digital signature signing and verification, and potentially for key creation (although I won't cry if I have to make a call to something else for that functionality). 我将使用该库进行数字签名签名和验证,并可能用于密钥创建(尽管如果我必须为此功能调用其他内容,我不会哭)。

I am using Python 3.x and have access to GPG. 我正在使用Python 3.x并可以访问GPG。

A new cryptography library for Python has been in rapid development for a few months now. 一个新的Python加密库已经快速发展了几个月了。 The 0.2.1 release just happened a few days ago. 0.2.1版本刚刚发布了几天前。

https://cryptography.io/en/latest/ https://cryptography.io/en/latest/

It is mainly a CFFI wrapper around existing C libraries such as OpenSSL. 它主要是围绕现有C库(如OpenSSL)的CFFI包装器。 It is distributed as a pure python module and supports CPython versions 2.6 - 3.3 as well as PyPy. 它作为纯python模块发布,支持CPython版本2.6 - 3.3以及PyPy。 It is also the upstream of the refactored pyOpenSSL package. 它也是重构的pyOpenSSL包的上游。

It aims to expose high-level "recipes" that makes cryptography as idiot-proof as possible as well as primitives that should only be used with the appropriate caution. 它的目的是揭示高级“配方”,使密码学尽可能具有愚蠢性,以及只应谨慎使用的原语。 Symmetric algorithms (including AES-GCM) is very well supported and asymmetric algorithms such as RSA and DSA should be coming in the next few releases. 对称算法(包括AES-GCM)得到了很好的支持,RSA和DSA等非对称算法应该会在接下来的几个版本中出现。 Other notable algorithms that are supported includes PBKDF2, HKDF, HOTP and TOTP. 支持的其他值得注意的算法包括PBKDF2,HKDF,HOTP和TOTP。

If you are in an environment which includes GnuPG and Python >= 2.4, then you could also consider a tool such as python-gnupg . 如果您所在的环境包含GnuPG和Python> = 2.4,那么您还可以考虑使用python-gnupg等工具。 (Disclaimer: I'm the maintainer of this project.) It leaves the heavy lifting to gpg and provides a fairly straightforward API. (免责声明:我是这个项目的维护者。)它为gpg留下了沉重的gpg并提供了一个相当简单的API。

Overview of API: API概述:

>>> import gnupg
>>> gpg = gnupg.GPG(gnupghome='/path/to/keyring/directory')
>>> gpg.list_keys()

[{
  ...
  'fingerprint': 'F819EE7705497D73E3CCEE65197D5DAC68F1AAB2',
  'keyid': '197D5DAC68F1AAB2',
  'length': '1024',
  'type': 'pub',
  'uids': ['', 'Gary Gross (A test user) ']},
 {
  ...
  'fingerprint': '37F24DD4B918CC264D4F31D60C5FEFA7A921FC4A',
  'keyid': '0C5FEFA7A921FC4A',
  'length': '1024',
  ...
  'uids': ['', 'Danny Davis (A test user) ']}]
>>> encrypted = gpg.encrypt("Hello, world!", ['0C5FEFA7A921FC4A'])
>>> str(encrypted)

'-----BEGIN PGP MESSAGE-----\nVersion: GnuPG v1.4.9 (GNU/Linux)\n
\nhQIOA/6NHMDTXUwcEAf
...
-----END PGP MESSAGE-----\n'
>>> decrypted = gpg.decrypt(str(encrypted), passphrase='secret')
>>> str(decrypted)
'Hello, world!'
>>> signed = gpg.sign("Goodbye, world!", passphrase='secret')
>>> verified = verified = gpg.verify(str(signed))
>>> print "Verified" if verified else "Not verified"

'Verified'

另一个需要考虑的加密库是PyCryptodome ,它是PyCrypto的一个支持PyPy和一些原语(SHA-3,Salsa20,scrypt等)。

pycrypt is actually a simple AES encrypt/decrypt module built on top of pycrypto like other modules you mention -- note that the latter is transitioning to the pycrypto.org URL as it's changing maintainers, and stable versions and docs are still at the original author's site . pycrypt实际上是一个简单的AES加密/解密模块,构建在pycrypto之上,就像你提到的其他模块一样 - 注意后者正在转换到pycrypto.org URL,因为它正在改变维护者,稳定的版本和文档仍然是原作者的网站 In addition to the easier-to-use wrappers you mention, one plus of pycrypto is that a pure-python subset of it is supplied with Google's App Engine, so getting familiar with it would be useful if you ever want to deploy any code there. 除了你提到的更容易使用的包装器之外,pycrypto的一个优点是它的纯python 子集是由Google的App Engine提供的,所以如果你想在那里部署任何代码,那么熟悉它会很有用。

The major alternative (another powerful and complex project, like pycrypto) is pyopenssl , which is a fairly regular wrapping (a "thin wrapper", as the author describes it) of OpenSSL (that may be a plus if you're used to coding in C with calls to OpenSSL). 主要的替代方案(另一个强大而复杂的项目,如pycrypto)是pyopenssl ,这是一个相当规则的包装(一个“薄包装”,如作者所描述的) OpenSSL (如果你习惯于编码,这可能是一个加号在C中调用OpenSSL)。 An alternative packaging that's complete (comes with the needed libraries) and possibly legally safer (excludes parts on which there are patent disputes or doubts) is distributed by egenix . egenix分发了一个完整的替代包装(附带所需的库),并且可能在法律上更安全(不包括存在专利纠纷或疑问的部分)。

Both main projects (pycrypto and pyopenssl) went through long periods of more or less inactivity as the original authors went on to other things, but both are actively developed and maintained again, which is always a good sign. 两个主要项目(pycrypto和pyopenssl)经历了或多或少的不活动,因为原始作者继续其他事情,但两者都积极开发和维护,这始终是一个好兆头。

I am not aware of easy-to-use wrappers on top of pyopenssl (there most likely are, but they haven't been publicized like those on top of pycrypto) and so, if as it seems you do care about ease of use and aren't looking to write wrappers yourself, the ones on top of pycrypto appear to be a better choice. 我不知道在pyopenssl之上易于使用的包装器(很可能是,但它们没有像pycrypto那样被公开)所以,如果看起来你似乎关心易用性和不想自己写包装,pycrypto之上的包装似乎是更好的选择。

I've just done such a survey last week and adopted M2Crypto that seems to be the most advanced wrapper today above openssl (found it in several recommandation lists while googling). 我上周刚刚完成了这样一项调查并采用了M2Crypto,它似乎是今天在openssl上面最先进的包装器(在google搜索时发现它在几个推荐列表中)。 I also tried pycrypto but it miss certificates management and standard key file format management that M2Crypto has (with pycrypto you have to pickle/unpicle your keys or write your own key manager for common formats). 我也试过pycrypto,但它错过了M2Crypto的证书管理和标准密钥文件格式管理(使用pycrypto你必须为你的密钥选择/取消密钥或为常见格式编写自己的密钥管理器)。

I found M2Crypto was quite easy to use and was quicly able to develop what I needed (a signed and encrypted package format). 我发现M2Crypto非常易于使用,并且能够开发出我需要的东西(签名和加密的包格式)。

However I recommand to download full package, not just easy installing it, because in the package you also get nice exemples (look at demo directory). 但是我建议下载完整的软件包,而不仅仅是简单安装它,因为在软件包中你也得到了很好的例子(看看demo目录)。

Here is the link http://pypi.python.org/pypi/M2Crypto/0.20.1 这是链接http://pypi.python.org/pypi/M2Crypto/0.20.1

A drawback could be that you are using python 3.0, I'm stuck with 2.5 at job (hopefully 2.6 soon) and don't know if M2Crypto works with python 3.0 一个缺点可能是你正在使用python 3.0,我在工作时遇到2.5(希望很快2.6)并且不知道M2Crypto是否适用于python 3.0

I've not much practice with it yet, put if you have specific problems with it just ask here. 我还没有多少练习,如果你有特殊的问题,请在这里问一下。 Someone may answer. 有人可能会回答。

PyCrypto is my choice atm (latest pypi update 2012-05-24) and the source code is hosted on GitHub: https://github.com/dlitz/pycrypto . PyCrypto是我的选择atm(最新的pypi更新2012-05-24),源代码托管在GitHub上: https//github.com/dlitz/pycrypto It can run pure Python math or use libgmp (you will need sudo apt-get install libgmp-dev on Debian to enable the latest). 它可以运行纯Python数学或使用libgmp (你需要在Debian上使用sudo apt-get install libgmp-dev来启用最新版本)。

M2Crypto is a wrapper for OpenSSL (latest pypi update 2011-01-15), source code at http://svn.osafoundation.org/m2crypto/ . M2Crypto是OpenSSL的包装器(最新的pypi更新2011-01-15),源代码位于http://svn.osafoundation.org/m2crypto/

gnupg (updated 2013-06-05), see Vinay Sajip's answer . gnupg (更新2013-06-05),请参阅Vinay Sajip的回答 There is a patched fork (updated 2013-07-31) hosted at https://github.com/isislovecruft/python-gnupg https://github.com/isislovecruft/python-gnupg上有一个修补的分支 (更新2013-07-31)

Other alternatives are mentioned by Alex Martelli Alex Martelli提到了其他替代方案

EDIT: critics of existing crypto packages and references to some new ones https://news.ycombinator.com/item?id=6194102 编辑:现有加密包的批评者和对一些新的加密包的引用https://news.ycombinator.com/item?id=6194102

Keyczar is cool, but it lacks OAEP|PKCS padding which is only avaliable in Java version. Keyczar很酷,但缺少OAEP | PKCS填充,这只能在Java版本中使用。 https://code.google.com/p/keyczar/wiki/KeyczarTool https://code.google.com/p/keyczar/wiki/KeyczarTool

Also, at the moment it lacks password based encryption which is avaliable in C++. 此外,目前它缺乏基于密码的加密,这在C ++中是可用的。 https://code.google.com/p/keyczar/issues/detail?id=149&colspec=ID%20Type%20Status%20Priority%20Milestone%20Owner%20Implementation%20Summary https://code.google.com/p/keyczar/issues/detail?id=149&colspec=ID%20Type%20Status%20Priority%20Milestone%20Owner%20Implementation%20Summary

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM