最近在写一个Python爬虫,翻了中英文的各种网站,都没有特别好用的AES工具类,特此写了一个,分享给大家。代码后面附有相关知识的讲解。
经测试,以下代码能正常运行,并且加密与解密结果,与http://tool.chacuo.net/cryptaes 或 https://blog.zhengxianjun.com/online-tool/crypto/aes 的一致
在使用前,需执行pip install pycryptodome
安装Crypto
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
import base64
'''
AES工具类:使用ECB模式加密,pkcs7格式的padding
为便于理解,我把每个步骤对应的代码单独成行了
'''
class AESUtil:
# 使用ECB模式加密
MODE = AES.MODE_ECB
# 使用默认的pkcs7 padding
PAD_STYLE = 'pkcs7'
ENCODING = 'UTF-8'
# key长度只能为16或24或32,分别对应AES-128、AES-192、AES-256
@staticmethod
def encrypt(plaintext: str, key: str) -> str:
# 将密钥编码为UTF-8格式的bytes
key_bytes = key.encode(AESUtil.ENCODING)
# 创建AES对象
cipher = AES.new(key_bytes, AESUtil.MODE)
# 将明文编码为UTF-8格式的bytes
plaintext_bytes = plaintext.encode(AESUtil.ENCODING)
# 为编码后的明文添加padding
plaintext_bytes_padded = pad(plaintext_bytes, AES.block_size, AESUtil.PAD_STYLE)
# 执行加密
ciphertext_bytes = cipher.encrypt(plaintext_bytes_padded)
# 将加密后的bytes进行base64编码
# 注意:不能用encodebytes!否则会每76个字符增加一个换行符,见:https://docs.python.org/zh-cn/3/library/base64.html
ciphertext_base64_bytes = base64.b64encode(ciphertext_bytes)
# 将base64编码过的bytes,解码为Python中使用的字符串类型(即unicode字符串)
ciphertext = ciphertext_base64_bytes.decode(AESUtil.ENCODING)
return ciphertext
@staticmethod
def decrypt(ciphertext: str, key: str) -> str:
# 将密钥编码为UTF-8格式的bytes
key_bytes = key.encode(AESUtil.ENCODING)
# 创建AES对象
decrypter = AES.new(key_bytes, AESUtil.MODE)
# 将密文编码为UTF-8格式的(同时也是base64编码的)bytes
ciphertext_base64_bytes = ciphertext.encode(AESUtil.ENCODING)
# 将base64编码的bytes,解码为原始的密文bytes
ciphertext_bytes = base64.b64decode(ciphertext_base64_bytes)
# 解码为明文
plaintext_bytes_padded = decrypter.decrypt(ciphertext_bytes)
# 去掉Padding
plaintext_bytes = unpad(plaintext_bytes_padded, AES.block_size, AESUtil.PAD_STYLE)
# 将UTF-8格式编码的明文bytes,解码为Python中的字符串类型(即unicode字符串)
plaintext = plaintext_bytes.decode(AESUtil.ENCODING)
return plaintext
相关知识讲解:
-
AES128/192/256:
分别对应:字节长度为16/24/32(128bit/192bit/256bit)的密钥。因此密钥长度必须严格符合,否则会报错。 -
ECB模式(Electronic Code Book):
先将明文分成若干个相同大小的块(如AES-128的为16字节),之后对每块使用相同的密钥独立加密获得N个密文块,不够安全,但便于并行,效率高 -
PKCS7 Padding:
见https://www.cnblogs.com/midea0978/articles/1437257.html
PKCS#7的定义:对于最后一部分不足区块长度的数据,若需要填充的字节的长度为剩余长度n,则需填充的每个字节的内容即为剩余长度n的十六进制。
举例,假定数据:FF FF FF FF FF FF FF FF FF(数据的长度为 9),块长度为16的情况下,
则需要填充16-9=7位数据,因此填充的字节也是07,用PKCS7填充后的结果:
FF FF FF FF FF FF FF FF FF 07 07 07 07 07 07 07 -
encode()和decode():
详见https://www.cnblogs.com/chaojiyingxiong/p/9822444.html
由于字符串在Python内部的表示是unicode编码,因此:
str1.encode('UTF-8')
,表示将unicode编码的str1转换成UTF-8编码。
bytes1.decode('UTF-8')
,表示将UTF-8编码的bytes1转换回unicode编码(即Python在内存中存储字符串的形式) -
为什么要用Base64:
AES加密后的密文是bytes形式,不可读;
而base64格式可以把bytes转化为字符串,方便在网络上传输,可读性也更好 -
为什么用
base64.encodebytes()
会产生多余的换行符\n?
见https://docs.python.org/zh-cn/3/library/base64.html
旧接口只支持标准的 Base64 字母表,并且按照 RFC 2045 的规范每 76 个字符增加一个换行符。注意:如果你需要支持RFC 2045,那么使用
参考:
https://www.cnblogs.com/linuxcat/p/14494630.html
https://blog.csdn.net/chouzhou9701/article/details/89339625
https://blog.csdn.net/weixin_43496689/article/details/104974432
https://www.cnblogs.com/xuchunlin/p/11421788.html
网友评论