The Crypto.Cipher package contains algorithms for protecting the confidentiality of data.
Crypto.Cipher包含保护机密数据的加密算法。
- 1
Installation(安装)
an almost drop-in replacement for the old PyCrypto library. You install it with:
替换旧版本PyCrypto库安装pip install pycryptodome
- 1
a library independent of the old PyCrypto. You install it with:
独立于旧版本PyCrypto库的安装pip install pycryptodomex
- 1
Introduction(介绍)
There are three types of encryption algorithms:
Crypto.Cipher包含三种加密算法
Symmetric ciphers: all parties use the same key, for both decrypting and encrypting data. Symmetric ciphers are typically very fast and can process very large amount of data.
对称加密算法:加解密数据使用同一秘钥,对称加密算法速度极快,能够快速处理非常庞大的数据。Asymmetric ciphers: senders and receivers use different keys. Senders encrypt with public keys (non-secret) whereas receivers decrypt with private keys (secret). Asymmetric ciphers are typically very slow and can process only very small payloads. Example: PKCS#1 OAEP (RSA).
非对称加密算法:发送者与接收者使用不同的秘钥,发送者使用公钥(非密)加密而接收者使用私钥解密。非对称加密算法速度很慢,只能够处理非常少的有效载荷(数据)。例如PKCS#1 OAEP (RSA)。Hybrid ciphers: the two types of ciphers above can be combined in a construction that inherits the benefits of both. An asymmetric cipher is used to protect a short-lived symmetric key, and a symmetric cipher (under that key) encrypts the actual message.
混合型加密算法:结合以上两种加密算法的优势。非对称加密算法经常被用来保护短活的对称秘钥,对称加密算法(使用对称秘钥)加密真实的信息。
基本的cipher API
You instantiate a cipher object by calling the new() function from the relevant cipher module (e.g. Crypto.Cipher.AES.new()). The first parameter is always the cryptographic key; its length depends on the particular cipher. You can (and sometimes must) pass additional cipher- or mode-specific parameters to new() (such as a nonce or a mode of operation).
通过从相关的cipher模块中调用new()函数实例化一个cipher对象(e.g. Crypto.Cipher.AES.new()),第一个参数是秘钥,长度取决于具体的加密算法,也可