本文主要是介绍python AES,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一,安装
win:
pip install pycryptodome
linux:
pip install pycrypto
二,AES - ECB模式
- 纯ecb,便于理解
import ostry:from Crypto.Cipher import AESfrom Crypto.Util.Padding import pad, unpad
except:os.system("pip install pycrypto")os.system("pip install pycryptodome")from Crypto.Cipher import AESfrom Crypto.Util.Padding import pad, unpadclass AesUtils:"""Aes[ECB]加解密"""@staticmethoddef ecb_encrypt(mingwen: str, key: str):"""ECB 加密"""# 补足指定字节数 Paddingpad_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@staticmethoddef 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,便于查看加密效果
import os import base64try:from Crypto.Cipher import AESfrom Crypto.Util.Padding import pad, unpad except:os.system("pip install pycrypto")os.system("pip install pycryptodome")from Crypto.Cipher import AESfrom Crypto.Util.Padding import pad, unpadclass AesUtils:"""Aes[ECB]加解密"""@staticmethoddef ecb_encrypt(mingwen: str, key: str):""" ECB 加密"""# 补足指定字节数 Paddingpad_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加密,并转成strbs_miwen = base64.encodebytes(miwen).decode()return bs_miwen@staticmethoddef 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,便于理解
#!/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 ostry:from Crypto.Cipher import AESfrom Crypto.Util.Padding import pad, unpad
except:os.system("pip install pycrypto")os.system("pip install pycryptodome")from Crypto.Cipher import AESfrom Crypto.Util.Padding import pad, unpadclass AesUtils:"""Aes[CBC]加解密"""@staticmethoddef cbc_encrypt(mingwen: str, key: str, iv: str):"""CBC 加密"""# iv必须是固定的16字节,这里不做填充iv = iv.encode()# key,mingwen 补字节 Paddingpwd_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)@staticmethoddef 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类型,转成strif __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,便于查看加密结果
#!/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 base64try:from Crypto.Cipher import AESfrom Crypto.Util.Padding import pad, unpad except:os.system("pip install pycrypto")os.system("pip install pycryptodome")from Crypto.Cipher import AESfrom Crypto.Util.Padding import pad, unpadclass AesUtils:"""Aes[CBC]加解密"""@staticmethoddef cbc_encrypt(mingwen: str, key: str, iv: str):"""CBC 加密"""# iv必须是固定的16字节,这里不做填充iv = iv.encode()# key,mingwen 补字节 Paddingpwd_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@staticmethoddef 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类型,转成strif __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)
这篇关于python AES的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!