1. AES算法简介
AES算法详解:高级加密标准,它是一种对称加密算法,AES只有一个密钥,这个密钥既用来加密,也用于解密。
AES加密方式有五种:ECB, CBC, CTR, CFB, OFB 。 从安全性角度推荐CBC加密方法,本文介绍了CBC ,ECB 两种加密方法的python实现。 CBC 和ECB模式的区别就是:
- CBC加密需要一个
十六位的key (密钥)和一个十六位iv (偏移量) - ECB加密不需要iv,只需要key(密钥)
2.模块安装
在 Windows下使用AES时要安装的是pycryptodome 模块
pip install pycryptodome
在 Linux下使用AES时要安装的是pycrypto模块
pip install pycrypto
3.AES的ECB 模式实现
第一种写法:
import base64
from Crypto.Cipher import AES
"""
ECB没有偏移量
"""
def pad(text):
"""
"""
count = len(text.encode('utf-8'))
add = 16 - (count % 16)
entext = text + (chr(add) * add)
return entext.encode('utf-8')
def add_to_16(text):
if len(text.encode('utf-8')) % 16:
add = 16 - (len(text.encode('utf-8')) % 16)
else:
add = 0
text = text + ('\0' * add)
return text.encode('utf-8')
def encrypt(text):
key = '9999999999999999'.encode('utf-8')
text = add_to_16(text)
cryptos = AES.new(key=key, mode=AES.MODE_ECB)
cipher_text = cryptos.encrypt(text)
msg = str(base64.b64encode(cipher_text), encoding="utf8")
return msg
def decrypt(text):
key = '9999999999999999'.encode('utf-8')
mode = AES.MODE_ECB
cryptor = AES.new(key, mode)
res = base64.decodebytes(text.encode("utf-8"))
plain_text = cryptor.decrypt(res).decode("utf-8").rstrip('\0')
return plain_text
if __name__ == '__main__':
text = 'happy_new_years_2022'
res = encrypt(text)
doc_text = decrypt(res)
print("加密前数据:", text)
print("加密后数据:", res)
print("数据解密:", doc_text)
运行结果: 第二种写法:
import base64
from Crypto.Cipher import AES
class EncryptDate:
def __init__(self, key):
self.key = key
self.length = 16
self.aes = AES.new(self.key, AES.MODE_ECB)
self.unpad = lambda date: date[0:-ord(date[-1])]
def pad(self, text):
"""
"""
count = len(text.encode('utf-8'))
add = self.length - (count % self.length)
entext = text + (chr(add) * add)
return entext
def encrypt(self, encrData):
a = self.pad(encrData)
res = self.aes.encrypt(a.encode("utf-8"))
msg = str(base64.b64encode(res), encoding="utf8")
return msg
def decrypt(self, decrData):
res = base64.decodebytes(decrData.encode("utf-8"))
msg = self.aes.decrypt(res).decode("utf-8")
return self.unpad(msg)
if __name__ == '__main__':
aes_key = "9999999999999999"
aes_text = "happy_new_years_2022"
eg = EncryptDate(aes_key.encode("utf-8"))
encrypt_data = eg.encrypt(aes_text)
print("明文数据数据:", eg.decrypt(encrypt_data))
print("加密后数据:", encrypt_data)
3.AES的CBC 模式实现
第一种写法:
import base64
from Crypto.Cipher import AES
def AES_Encrypt(key, data, vi):
pad = lambda s: s + (16 - len(s) % 16) * chr(16 - len(s) % 16)
data = pad(data)
cipher = AES.new(key.encode('utf8'), AES.MODE_CBC, vi.encode('utf8'))
encryptedbytes = cipher.encrypt(data.encode('utf8'))
encodestrs = base64.b64encode(encryptedbytes)
enctext = encodestrs.decode('utf8')
return enctext
def AES_Decrypt(key, data, vi):
data = data.encode('utf8')
res = base64.decodebytes(data)
cipher = AES.new(key.encode('utf8'), AES.MODE_CBC, vi.encode('utf8'))
text_decrypted = cipher.decrypt(res)
unpad = lambda s: s[0:-s[-1]]
text_decrypted = unpad(text_decrypted)
text_decrypted = text_decrypted.decode('utf8')
return text_decrypted
if __name__ == '__main__':
aes_key = '0CoJUm6Qyw8W8jud'
aes_iv = "9999999999999999"
data_text = 'dsdadaxdgdfgdfgwer tw4356 45623 462345fas'
enctext = AES_Encrypt(aes_key, data_text, aes_iv)
text_decrypted = AES_Decrypt(aes_key, enctext, aes_iv)
print("原始文本:", data_text)
print("AES加密后:", enctext)
print("AES解密后:", text_decrypted)
效果如图: 第二种写法:
import base64
from Crypto.Cipher import AES
class EncryptDate:
def __init__(self, key, iv):
self.key = key.encode("utf-8")
self.iv = iv.encode("utf-8")
self.length = 16
self.aes = AES.new(self.key, AES.MODE_CBC, self.iv)
self.unpad = lambda s: s[0:-s[-1]]
def pad(self, text):
"""
填充函数,使被加密数据的字节码长度是block_size的整数倍
"""
count = len(text.encode('utf-8'))
add = self.length - (count % self.length)
entext = text + (chr(add) * add)
return entext
def encrypt(self, encrData):
a = self.pad(encrData)
res = self.aes.encrypt(a.encode("utf-8"))
msg = str(base64.b64encode(res), encoding="utf8")
return msg
def decrypt(self, decrData):
res = base64.decodebytes(decrData.encode("utf-8"))
msg_text = self.aes.decrypt(res)
decrypt_text = self.unpad(msg_text).decode('utf8')
return decrypt_text
if __name__ == '__main__':
aes_key = "0CoJUm6Qyw8W8jud"
aes_iv = "9999999999999999"
text_data = 'dsdadaxdgdfgdfgwer tw4356 45623 462345fas'
encrypt_data = EncryptDate(aes_key, aes_iv).encrypt(text_data)
decrypt_data = EncryptDate(aes_key, aes_iv).decrypt(encrypt_data)
print("原始数据:", text_data)
print("加密后数据:", encrypt_data)
print("数据解密:", decrypt_data)
|