python 实现aes256加密
基础知识
#在Linux操作系统下,Python3的默认环境编码变为了utf-8编码,所以在编写代码的时候,字符串大部分都是以utf-8处理
UTF-8:
1byte=8bit
1个英文字符=1byte
1个中文字符=3byte
128bit=16byte=16个英文字符
192bit=24byte=24个英文字符
256bit=32byte=32个英文字符
AES256概念
AES是一种对称加密算法,对称指加密和解密使用同一个密钥;256指密钥的长度是256bit,即32个英文字符的长度;密钥的长度决定了AES加密的轮数
AES256加密参数
- 密钥:一个32byte的字符串,常被叫为key
- 明文:待加密的字符串;字节长度(按byte计算)必须是16的整数倍,因此,明文加密之前需要被填充
- 模式:加密模式,常用的有ECB、CBC;具体含义见参考链接
- iv偏移量:CBC模式下需要是16byte字符串;ECB下不需要
参考代码
#-------------------------------
#-*-coding:utf-8-*-
#@Author:jianghan
#@Time:2020/11/2514:46
#@File:crypt.py
#Python版本:3.6.8
#-------------------------------
"""
1、填充字符串和明文字符串最后一位不能相同
2、字符串编码默认是utf-8,key和iv默认为英文字符;字符串不支持其他编码或key/iv不支持为中文字符
"""
fromenumimportEnum,unique
fromCrypto.CipherimportAES
@unique
classMode(Enum):
CBC=AES.MODE_CBC
ECB=AES.MODE_ECB
@unique
classPadding(Enum):
"""定义填充的字符串"""
SPACE=''#空格
classAES256Crypto:
def__init__(self,key,mode=Mode.ECB,padding=Padding.SPACE,iv=None):
"""
:paramkey:密钥,32byte长度字符串
:parammode:加密模式,来源classMode
:paramiv:16byte长度字符串
:parampadding:填充的字符串,来源classPadding
"""
self.padding=self.check_padding(padding)
self.key=self.padding_key(key)
self.iv=self.padding_iv(iv)ifivelseNone
self.mode=self.check_mode(mode)
defcheck_mode(self,mode):
"""核对mode"""
ifmodenotinMode.__members__.values():
raiseException(f'mode{mode}notallowed!')
ifmode==Mode.CBCandnotself.iv:
raiseException(f'ivisrequired')
returnmode
defcheck_padding(self,padding):
"""核对padding"""
ifpaddingnotinPadding.__members__.values():
raiseException(f'mode{padding}notallowed!')
returnpadding
defpadding_ret_byte(self,text,_len=16):
"""填充并转成bytes"""
text=text.encode()
remainder=len(text)%_len
remainder=_lenifremainder==0elseremainder
text+=(_len-remainder)*self.padding.value.encode()
returntext
defpadding_iv(self,iv:str):
"""补全iv并转成bytes"""
iflen(iv.encode())>16:
raiseException(f'iv{iv}must<=16bytes')
returnself.padding_ret_byte(iv)
defpadding_key(self,key:str):
"""补全key并转成bytes"""
iflen(key.encode())>32:
raiseException(f'key{key}must<=32bytes')
returnself.padding_ret_byte(key,_len=32)
defencrypt(self,text,encode=None):
"""
加密
:paramtext:待加密字符串
:paramencode:传入base64里面的方法
:return:若encode=None则不进行base加密处理,返回bytes类型数据
"""
text=self.padding_ret_byte(text)
#注意:加密中的和解密中的AES.new()不能使用同一个对象,所以在两处都使用了AES.new()
text=AES.new(key=self.key,mode=self.mode.value,iv=self.iv).encrypt(text)
ifencode:
returnencode(text).decode()
returntext
defdecrypt(self,text,decode=None):
"""解密"""
ifdecode:
iftype(text)==str:
text=text.encode()
text=decode(bytes(text))
else:
iftype(text)!=bytes:
raiseException(text)
text=AES.new(key=self.key,mode=self.mode.value,iv=self.iv).decrypt(text)
text=text.strip(self.padding.value.encode())
returntext.decode()
使用范例
importjson
#这是一段待加密的字符串
text='{"upi":"1341343","overdue":"2020-11-2600:00:00"}'
key='t6LtKa3tD5X6qaJ6qOrAW3XmobFrY6ob'
iv='NjtP47eSECuOm3s6'
aes=AES256Crypto(key,Mode.CBC,Padding.SPACE,iv)
text_1=aes.encrypt(text)
#b'\xe7\x1d\xeae\xff\xc7\xc2\xd7\x8c\xf6\xe7\x82u\x7f\x168\xbc\x90\xad\x1e\x85M\xcb\xb0\xb4Ho\x1b\xe4\xec\x9d\x1d\xf93\xeb\x9b\xe7\xa3\xdd$\x8cEa\xab\xf7K~\x91H\xc3]5\xc4\x1a\xd4w[\x83\xb2"FC\x9f\x9d'
text_2=aes.decrypt(text_1)
#'{"upi":"1341343","overdue":"2020-11-2600:00:00"}'
importbase64
text_3=aes.encrypt(text,encode=base64.b16encode)
#'E71DEA65FFC7C2D78CF6E782757F1638BC90AD1E854DCBB0B4486F1BE4EC9D1DF933EB9BE7A3DD248C4561ABF74B7E9148C35D35C41AD4775B83B22246439F9D'
text_4=aes.decrypt(text_3,decode=base64.b16decode)
#'{"upi":"1341343","overdue":"2020-11-2600:00:00"}'
以上就是python实现aes256加密的详细内容,更多关于pythonaes256加密的资料请关注毛票票其它相关文章!