Detailed explanation of encryption and decryption in Python3

巴扎黑
Release: 2017-08-21 14:42:51
Original
13679 people have browsed it

The standard library of Python 3 does not have much to deal with encryption, but there is a library for processing hashes. Here we will give a brief introduction to it, but the focus will be on two third-party software packages: PyCrypto and cryptography. We will learn how to use these two libraries to encrypt and decrypt strings.

Hash

If you need to use a secure hash algorithm or message digest algorithm, then you can use the hashlib module in the standard library. This module contains FIPS (Federal Information Processing Standard) compliant secure hashing algorithms, including SHA1, SHA224, SHA256, SHA384, SHA512 and RSA's MD5 algorithm. Python also supports adler32 and crc32 hash functions, but they are in the zlib module.

One of the most common uses of hashes is to store the hash value of a password rather than the password itself. Of course, the hash function used needs to be more robust, otherwise it will be easily cracked. Another common usage is to calculate the hash of a file and then send the file and its hash separately. The person receiving the file can calculate the hash value of the file and check whether it matches the hash value received. If the two match, it means that the file has not been tampered with during transmission.

Let’s try creating an md5 hash:

>>> import hashlib
>>> md5 = hashlib.md5()
>>> md5.update('Python rocks!')
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    md5.update(&#39;Python rocks!&#39;)
TypeError: Unicode-objects must be encoded before hashing
>>> md5.update(b&#39;Python rocks!&#39;)
>>> md5.digest()
b&#39;\x14\x82\xec\x1b#d\xf6N}\x16*+[\x16\xf4w&#39;
Copy after login

Let’s take a moment to explain it line by line. First, we import hashlib and then create an instance of the md5 hash object. Then, after we added a string to this instance, we got an error message. It turns out that when calculating the md5 hash, you need to use a string in byte form instead of an ordinary string. After adding the string correctly, we call its digest function to get the hash value. If you want a hexadecimal hash value, you can also use the following method:

>>> md5.hexdigest()
&#39;1482ec1b2364f64e7d162a2b5b16f477&#39;
Copy after login

Actually, there is a streamlined way to create a hash, let's take a look at creating a sha1 using this method Hash:

>>> sha = hashlib.sha1(b&#39;Hello Python&#39;).hexdigest()
>>> sha
&#39;422fbfbc67fe17c86642c5eaaa48f8b670cbed1b&#39;
Copy after login

As you can see, we can create a hash instance and call its digest function at the same time. Then, we print out the hash value and take a look. Here I use the sha1 hash function as an example, but it is not particularly secure and readers are free to try other hash functions.

Key Export

Python’s standard library has weak support for key export. In fact, the only method provided by the hashlib library is the pbkdf2_hmac function. It is the second password-based key derivation function of PKCS#5 and uses HMAC as the pseudorandom function. Because it supports "salting" and iterative operations, you can use similar methods to hash your passwords. For example, if you plan to use the SHA-256 encryption method, you will need a "salt" of at least 16 bytes, and a minimum of 100,000 iterations.

Simply put, "salt" is random data that is added to the hashing process to increase the difficulty of cracking. This basically protects your passwords from dictionary and rainbow table attacks.

Let's look at a simple example:

>>> import binascii
>>> dk = hashlib.pbkdf2_hmac(hash_name=&#39;sha256&#39;,
        password=b&#39;bad_password34&#39;, 
        salt=b&#39;bad_salt&#39;, 
        iterations=100000)
>>> binascii.hexlify(dk)
b&#39;6e97bad21f6200f9087036a71e7ca9fa01a59e1d697f7e0284cd7f9b897d7c02&#39;
Copy after login

Here we're hashing a password with SHA256, using a bad salt, but over 100,000 iterations. Of course, SHA is not actually recommended for creating password keys. You should use an algorithm like scrypt instead. Another good option is to use a third-party library called bcrypt, which is specifically designed to hash passwords.

PyCryptodome

PyCrypto is probably the most famous third-party package for cryptography in Python. Unfortunately, its development ceased in 2012. Others continue to release the latest version of PyCrypto, and if you don't mind using third-party binary packages, you can still get the corresponding version for Python 3.5. For example, I found the PyCrypto binary package corresponding to Python 3.5 on Github (https://github.com/sfbahr/PyCrypto-Wheels).

Fortunately, there is a fork of the project called PyCrytodome that replaced PyCrypto. In order to install it on Linux, you can use the following pip command:

pip install pycryptodome
Copy after login

Installation on a Windows system is slightly different:

pip install pycryptodomex
Copy after login

If you encounter problems, it may be because you did not install it The correct dependency package (LCTT annotation: such as python-devel), or your Windows system requires a compiler.

It’s also worth noting that PyCryptodome has many improvements over the last version of PyCrypto. It’s well worth visiting their homepage to see what new features are available.

Encrypted String

After visiting their homepage, we can look at some examples. In the first example, we will use the DES algorithm to encrypt a string:

>>> from Crypto.Cipher import DES
>>> key = &#39;abcdefgh&#39;
>>> def pad(text):
        while len(text) % 8 != 0:
            text += &#39; &#39;
        return text
>>> des = DES.new(key, DES.MODE_ECB)
>>> text = &#39;Python rocks!&#39;
>>> padded_text = pad(text)
>>> encrypted_text = des.encrypt(text)
Traceback (most recent call last):
  File "<pyshell#35>", line 1, in <module>
    encrypted_text = des.encrypt(text)
  File "C:\Programs\Python\Python35-32\lib\site-packages\Crypto\Cipher\blockalgo.py", line 244, in encrypt
    return self._cipher.encrypt(plaintext)
ValueError: Input strings must be a multiple of 8 in length
>>> encrypted_text = des.encrypt(padded_text)
>>> encrypted_text
b&#39;>\xfc\x1f\x16x\x87\xb2\x93\x0e\xfcH\x02\xd59VQ&#39;
Copy after login

这段代码稍有些复杂,让我们一点点来看。首先需要注意的是,DES 加密使用的密钥长度为 8 个字节,这也是我们将密钥变量设置为 8 个字符的原因。而我们需要加密的字符串的长度必须是 8 的倍数,所以我们创建了一个名为 pad 的函数,来给一个字符串末尾填充空格,直到它的长度是 8 的倍数。然后,我们创建了一个 DES 的实例,以及我们需要加密的文本。我们还创建了一个经过填充处理的文本。我们尝试着对未经填充处理的文本进行加密,啊欧,报了一个 ValueError 错误!我们需要对经过填充处理的文本进行加密,然后得到加密的字符串。(LCTT 译注:encrypt 函数的参数应为 byte 类型字符串,代码为:encrypted_text = des.encrypt(padded_text.encode('utf-8')))

知道了如何加密,还要知道如何解密:

>>> des.decrypt(encrypted_text)
b&#39;Python rocks!   &#39;
Copy after login

幸运的是,解密非常容易,我们只需要调用 des 对象的 decrypt 方法就可以得到我们原来的 byte 类型字符串了。下一个任务是学习如何用 RSA 算法加密和解密一个文件。首先,我们需要创建一些 RSA 密钥。

创建 RSA 密钥

如果你希望使用 RSA 算法加密数据,那么你需要拥有访问 RAS 公钥和私钥的权限,否则你需要生成一组自己的密钥对。在这个例子中,我们将生成自己的密钥对。创建 RSA 密钥非常容易,所以我们将在 Python 解释器中完成。

>>> from Crypto.PublicKey import RSA
>>> code = &#39;nooneknows&#39;
>>> key = RSA.generate(2048)
>>> encrypted_key = key.exportKey(passphrase=code, pkcs=8, 
        protection="scryptAndAES128-CBC")
>>> with open(&#39;/path_to_private_key/my_private_rsa_key.bin&#39;, &#39;wb&#39;) as f:
        f.write(encrypted_key)
>>> with open(&#39;/path_to_public_key/my_rsa_public.pem&#39;, &#39;wb&#39;) as f:
        f.write(key.publickey().exportKey())
Copy after login

首先我们从 Crypto.PublicKey 包中导入 RSA,然后创建一个傻傻的密码。接着我们生成 2048 位的 RSA 密钥。现在我们到了关键的部分。为了生成私钥,我们需要调用 RSA 密钥实例的 exportKey 方法,然后传入密码,使用的 PKCS 标准,以及加密方案这三个参数。之后,我们把私钥写入磁盘的文件中。

接下来,我们通过 RSA 密钥实例的 publickey 方法创建我们的公钥。我们使用方法链调用 publickey 和 exportKey 方法生成公钥,同样将它写入磁盘上的文件。

加密文件

有了私钥和公钥之后,我们就可以加密一些数据,并写入文件了。这里有个比较标准的例子:

from Crypto.PublicKey import RSA
from Crypto.Random import get_random_bytes
from Crypto.Cipher import AES, PKCS1_OAEP
with open(&#39;/path/to/encrypted_data.bin&#39;, &#39;wb&#39;) as out_file:
    recipient_key = RSA.import_key(
        open(&#39;/path_to_public_key/my_rsa_public.pem&#39;).read())
    session_key = get_random_bytes(16)
    cipher_rsa = PKCS1_OAEP.new(recipient_key)
    out_file.write(cipher_rsa.encrypt(session_key))
    cipher_aes = AES.new(session_key, AES.MODE_EAX)
    data = b&#39;blah blah blah Python blah blah&#39;
    ciphertext, tag = cipher_aes.encrypt_and_digest(data)
    out_file.write(cipher_aes.nonce)
    out_file.write(tag)
    out_file.write(ciphertext)
Copy after login

代码的前三行导入 PyCryptodome 包。然后我们打开一个文件用于写入数据。接着我们导入公钥赋给一个变量,创建一个 16 字节的会话密钥。在这个例子中,我们将使用混合加密方法,即 PKCS#1 OAEP ,也就是最优非对称加密填充。这允许我们向文件中写入任意长度的数据。接着我们创建 AES 加密,要加密的数据,然后加密数据。我们将得到加密的文本和消息认证码。最后,我们将随机数,消息认证码和加密的文本写入文件。

顺便提一下,随机数通常是真随机或伪随机数,只是用来进行密码通信的。对于 AES 加密,其密钥长度最少是 16 个字节。随意用一个你喜欢的编辑器试着打开这个被加密的文件,你应该只能看到乱码。

现在让我们学习如何解密我们的数据。

from Crypto.PublicKey import RSA
from Crypto.Cipher import AES, PKCS1_OAEP
code = &#39;nooneknows&#39;
with open(&#39;/path/to/encrypted_data.bin&#39;, &#39;rb&#39;) as fobj:
    private_key = RSA.import_key(
        open(&#39;/path_to_private_key/my_rsa_key.pem&#39;).read(),
        passphrase=code)
    enc_session_key, nonce, tag, ciphertext = [ fobj.read(x) 
                                                for x in (private_key.size_in_bytes(), 
                                                16, 16, -1) ]
    cipher_rsa = PKCS1_OAEP.new(private_key)
    session_key = cipher_rsa.decrypt(enc_session_key)
    cipher_aes = AES.new(session_key, AES.MODE_EAX, nonce)
    data = cipher_aes.decrypt_and_verify(ciphertext, tag)
print(data)
Copy after login

如果你认真看了上一个例子,这段代码应该很容易解析。在这里,我们先以二进制模式读取我们的加密文件,然后导入私钥。注意,当你导入私钥时,需要提供一个密码,否则会出现错误。然后,我们文件中读取数据,首先是加密的会话密钥,然后是 16 字节的随机数和 16 字节的消息认证码,最后是剩下的加密的数据。

接下来我们需要解密出会话密钥,重新创建 AES 密钥,然后解密出数据。

你还可以用 PyCryptodome 库做更多的事。不过我们要接着讨论在 Python 中还可以用什么来满足我们加密解密的需求。

cryptography 包

cryptography 的目标是成为“人类易于使用的密码学包cryptography for humans”,就像 requests 是“人类易于使用的 HTTP 库HTTP for Humans”一样。这个想法使你能够创建简单安全、易于使用的加密方案。如果有需要的话,你也可以使用一些底层的密码学基元,但这也需要你知道更多的细节,否则创建的东西将是不安全的。

如果你使用的 Python 版本是 3.5, 你可以使用 pip 安装,如下:

pip install cryptography
Copy after login

你会看到 cryptography 包还安装了一些依赖包(LCTT 译注:如 libopenssl-devel)。如果安装都顺利,我们就可以试着加密一些文本了。让我们使用 Fernet 对称加密算法,它保证了你加密的任何信息在不知道密码的情况下不能被篡改或读取。Fernet 还通过 MultiFernet 支持密钥轮换。下面让我们看一个简单的例子:

>>> from cryptography.fernet import Fernet
>>> cipher_key = Fernet.generate_key()
>>> cipher_key
b&#39;APM1JDVgT8WDGOWBgQv6EIhvxl4vDYvUnVdg-Vjdt0o=&#39;
>>> cipher = Fernet(cipher_key)
>>> text = b&#39;My super secret message&#39;
>>> encrypted_text = cipher.encrypt(text)
>>> encrypted_text
(b&#39;gAAAAABXOnV86aeUGADA6mTe9xEL92y_m0_TlC9vcqaF6NzHqRKkjEqh4d21PInEP3C9HuiUkS9f&#39;
 b&#39;6bdHsSlRiCNWbSkPuRd_62zfEv3eaZjJvLAm3omnya8=&#39;)
>>> decrypted_text = cipher.decrypt(encrypted_text)
>>> decrypted_text
b&#39;My super secret message&#39;
Copy after login

首先我们需要导入 Fernet,然后生成一个密钥。我们输出密钥看看它是什么样儿。如你所见,它是一个随机的字节串。如果你愿意的话,可以试着多运行 generate_key 方法几次,生成的密钥会是不同的。然后我们使用这个密钥生成 Fernet 密码实例。

现在我们有了用来加密和解密消息的密码。下一步是创建一个需要加密的消息,然后使用 encrypt 方法对它加密。我打印出加密的文本,然后你可以看到你再也读不懂它了。为了解密出我们的秘密消息,我们只需调用 decrypt 方法,并传入加密的文本作为参数。结果就是我们得到了消息字节串形式的纯文本。

小结

这一章仅仅浅显地介绍了 PyCryptodome 和 cryptography 这两个包的使用。不过这也确实给了你一个关于如何加密解密字符串和文件的简述。请务必阅读文档,做做实验,看看还能做些什么!

The above is the detailed content of Detailed explanation of encryption and decryption in Python3. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template