python--AES加密

AES加密,用的是第三方模块 pycryptodome 

 安装:pip install pycryptodome -i https://pypi.douban.com/simple  

ECB模式加密: 

from Crypto.Cipher import AES
import base64

password = '1234567890123456'.encode() #秘钥
#秘钥:必须是16位字节或者24位字节或者32位字节(因为python3的字符串是unicode编码,需要 encode才可以转换成字节型数据)

text = 'wo is liming' #需要加密的内容

while len(text) % 16 != 0:  #处理数据必须与块边界对齐
        text += '\0'
text=text.encode()

model = AES.MODE_ECB #定义模式
#模式:
#电码本模式(Electronic Codebook Book (ECB))
#密码分组链接模式(Cipher Block Chaining (CBC))
#计算器模式(Counter (CTR))
#密码反馈模式(Cipher FeedBack (CFB))
#输出反馈模式(Output FeedBack (OFB))

aes = AES.new(password,model) #创建一个aes对象

en_text = aes.encrypt(text) #加密明文
#b'\x0b\x0f.\x1fc\x83-/\xac\x04#\x89Qs\x8c\xec'

en_text = base64.encodebytes(en_text) #将返回的字节型数据转进行base64编码
#b'Cw8uH2ODLS+sBCOJUXOM7A==\n'
en_text = en_text.decode('utf8') #转换成python中的字符串类型
#Cw8uH2ODLS+sBCOJUXOM7A==

print(en_text)

猜你喜欢

转载自www.cnblogs.com/liming19680104/p/12171198.html
今日推荐