一,安装
win:
javascript
pip install pycryptodome
linux:
bash
pip install pycrypto
二,AES - ECB模式
- 纯ecb,便于理解
python
import os
try:
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
except:
os.system("pip install pycrypto")
os.system("pip install pycryptodome")
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
class AesUtils:
"""Aes[ECB]加解密"""
@staticmethod
def ecb_encrypt(mingwen: str, key: str):
"""ECB 加密"""
# 补足指定字节数 Padding
pad_key = pad(key.encode(), block_size=AES.key_size[2], style="pkcs7")
pad_mingwen = pad(mingwen.encode(), block_size=AES.block_size, style="pkcs7")
# 加密
obj = AES.new(pad_key, AES.MODE_ECB)
miwen = obj.encrypt(pad_mingwen)
return miwen
@staticmethod
def ecb_decrypt(miwen: bytes, key: str):
"""ECB 解密"""
# 解除加密时对key的补充
pad_key = pad(key.encode(), block_size=AES.key_size[2], style="pkcs7")
# 解密
obj = AES.new(pad_key, AES.MODE_ECB)
pad_mingwen = obj.decrypt(miwen)
# 解除加密时对明文的填充
mingwen = unpad(pad_mingwen, block_size=AES.block_size, style="pkcs7")
return mingwen.decode()
if __name__ == "__main__":
# 一个汉字两个字节
text = "我是需要加密的明文"
# Key 必须是16字节(*AES-128*), 24字节(*AES-192*),32字节(*AES-256*)
key = "abcdfeghijklmnop"
#####################################################################
# 加密过程中,对key和mingwen进行了填充,需要返回来用于解除填充后用于解密
miwen = AesUtils.ecb_encrypt(mingwen=text, key=key)
print("加密:", miwen)
#####################################################################
decrypted = AesUtils.ecb_decrypt(miwen=miwen, key=key)
print("解密:", decrypted)
-
ecb+base64,便于查看加密效果
pythonimport os import base64 try: from Crypto.Cipher import AES from Crypto.Util.Padding import pad, unpad except: os.system("pip install pycrypto") os.system("pip install pycryptodome") from Crypto.Cipher import AES from Crypto.Util.Padding import pad, unpad class AesUtils: """Aes[ECB]加解密""" @staticmethod def ecb_encrypt(mingwen: str, key: str): """ ECB 加密""" # 补足指定字节数 Padding pad_key = pad(key.encode(), block_size=AES.key_size[2], style="pkcs7") pad_mingwen = pad(mingwen.encode(), block_size=AES.block_size, style="pkcs7") # 加密 obj = AES.new(pad_key, AES.MODE_ECB) miwen = obj.encrypt(pad_mingwen) # base64对字节密文进行b64加密,并转成str bs_miwen = base64.encodebytes(miwen).decode() return bs_miwen @staticmethod def ecb_decrypt(bs_miwen: str, key: str): """ ECB 解密""" # 解除加密时对key的补充 pad_key = pad(key.encode(), block_size=AES.key_size[2], style="pkcs7") # 将b64的str明文先转成bytes,再b64解密 miwen = base64.decodebytes(bs_miwen.encode()) # 解密 obj = AES.new(pad_key, AES.MODE_ECB) pad_mingwen = obj.decrypt(miwen) # 解除加密时对明文的填充 mingwen = unpad(pad_mingwen, block_size=AES.block_size, style="pkcs7") return mingwen.decode() if __name__ == "__main__": # 一个汉字两个字节 text = "我是需要加密的明文" # Key 必须是16字节(*AES-128*), 24字节(*AES-192*),32字节(*AES-256*) key = "abcdfeghijklmnop" ##################################################################### # 加密过程中,对key和mingwen进行了填充,需要返回来用于解除填充后用于解密 bs_miwen = AesUtils.ecb_encrypt(mingwen=text, key=key) print("加密:", bs_miwen) ##################################################################### decrypted = AesUtils.ecb_decrypt(bs_miwen=bs_miwen, key=key) print("解密:", decrypted)
三,AES - CBC
-
纯cbc,便于理解
python
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@CreateDate: 2024/08/28 12:18:11
@modifyDate:
@Author: Yang·MaoMao
@Version: 1.0.0.0
@summary: Aes[CBC]加解密
"""
import os
try:
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
except:
os.system("pip install pycrypto")
os.system("pip install pycryptodome")
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
class AesUtils:
"""Aes[CBC]加解密"""
@staticmethod
def cbc_encrypt(mingwen: str, key: str, iv: str):
"""CBC 加密"""
# iv必须是固定的16字节,这里不做填充
iv = iv.encode()
# key,mingwen 补字节 Padding
pwd_key = pad(key.encode(), block_size=AES.key_size[1], style="pkcs7")
pad_mingwen = pad(mingwen.encode(), block_size=AES.block_size, style="pkcs7")
# 加密
obj = AES.new(pwd_key, AES.MODE_CBC, iv=iv)
return obj.encrypt(pad_mingwen)
@staticmethod
def cbc_decrypt(miwen: bytes, key: str, iv: str):
"""CBC 解密"""
iv = iv.encode()
#填充key是保证跟加密时填充过的key一致
key = pad(key.encode(), block_size=AES.key_size[1], style="pkcs7")
# 解密
obj = AES.new(key, AES.MODE_CBC, iv=iv)
pad_mingwen = obj.decrypt(miwen)
# 解密得到的是加密时填充过的明文,所以需要解除填充
mingwen = unpad(pad_mingwen, block_size=AES.block_size, style="pkcs7")
return mingwen.decode() # 解除填充是bytes类型,转成str
if __name__ == "__main__":
text = "我是需要加密的明文" # 一个汉字两个字节
# Key 必须是16字节(*AES-128*), 24字节(*AES-192*),32字节(*AES-256*)
key = "abcdfeghijklmnopabcd"
# IV 必!须!是!16字节 , 适用于CBC模式
iv = "abcdfeghijklmnop"
#####################################################################
miwen = AesUtils.cbc_encrypt(mingwen=text, key=key, iv=iv)
print("加密:", miwen)
mingwen = AesUtils.cbc_decrypt(miwen=miwen, key=key, iv=iv)
print("解密:", mingwen)
-
cbc+base64,便于查看加密结果
python#!/usr/bin/env python # -*- encoding: utf-8 -*- """ @CreateDate: 2024/08/28 12:18:11 @modifyDate: @Author: Yang·MaoMao @Version: 1.0.0.0 @summary: Aes[CBC]加解密 """ import os import base64 try: from Crypto.Cipher import AES from Crypto.Util.Padding import pad, unpad except: os.system("pip install pycrypto") os.system("pip install pycryptodome") from Crypto.Cipher import AES from Crypto.Util.Padding import pad, unpad class AesUtils: """Aes[CBC]加解密""" @staticmethod def cbc_encrypt(mingwen: str, key: str, iv: str): """CBC 加密""" # iv必须是固定的16字节,这里不做填充 iv = iv.encode() # key,mingwen 补字节 Padding pwd_key = pad(key.encode(), block_size=AES.key_size[1], style="pkcs7") pad_mingwen = pad(mingwen.encode(), block_size=AES.block_size, style="pkcs7") # 加密 obj = AES.new(pwd_key, AES.MODE_CBC, iv=iv) miwen = obj.encrypt(pad_mingwen) bs_miwen = base64.encodebytes(miwen).decode() return bs_miwen @staticmethod def cbc_decrypt(bs_miwen: str, key: str, iv: str): """CBC 解密""" iv = iv.encode() #填充key是保证跟加密时填充过的key一致 key = pad(key.encode(), block_size=AES.key_size[1], style="pkcs7") # base64解密 miwen = base64.decodebytes(bs_miwen.encode()) # 解密 obj = AES.new(key, AES.MODE_CBC, iv=iv) pad_mingwen = obj.decrypt(miwen) # 解密得到的是加密时填充过的明文,所以需要解除填充 mingwen = unpad(pad_mingwen, block_size=AES.block_size, style="pkcs7") return mingwen.decode() # 解除填充是bytes类型,转成str if __name__ == "__main__": text = "我是需要加密的明文" # 一个汉字两个字节 # Key 必须是16字节(*AES-128*), 24字节(*AES-192*),32字节(*AES-256*) key = "abcdfeghijklmnopabcd" # IV 必!须!是!16字节 , 适用于CBC模式 iv = "abcdfeghijklmnop" ##################################################################### bs_miwen = AesUtils.cbc_encrypt(mingwen=text, key=key, iv=iv) print("加密:", bs_miwen) mingwen = AesUtils.cbc_decrypt(bs_miwen=bs_miwen, key=key, iv=iv) print("解密:", mingwen)