新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

Python如何实现AES加密与解密-创新互联

这期内容当中小编将会给大家带来有关Python如何实现AES加密与解密,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

勐海网站建设公司成都创新互联,勐海网站设计制作,有大型网站制作公司丰富经验。已为勐海近1000家提供企业网站建设服务。企业网站搭建\外贸网站建设要多少钱,请找那个售后服务好的勐海做网站的公司定做!

第一种

import base64
from Crypto.Cipher import AES


# 密钥(key), 密斯偏移量(iv) CBC模式加密

def AES_Encrypt(key, data):
  vi = '0102030405060708'
  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'))
  # 加密后得到的是bytes类型的数据
  encodestrs = base64.b64encode(encryptedbytes)
  # 使用Base64进行编码,返回byte字符串
  enctext = encodestrs.decode('utf8')
  # 对byte字符串按utf-8进行解码
  return enctext


def AES_Decrypt(key, data):
  vi = '0102030405060708'
  data = data.encode('utf8')
  encodebytes = base64.decodebytes(data)
  # 将加密数据转换位bytes类型数据
  cipher = AES.new(key.encode('utf8'), AES.MODE_CBC, vi.encode('utf8'))
  text_decrypted = cipher.decrypt(encodebytes)
  unpad = lambda s: s[0:-s[-1]]
  text_decrypted = unpad(text_decrypted)
  # 去补位
  text_decrypted = text_decrypted.decode('utf8')
  return text_decrypted


key = '0CoJUm6Qyw8W8jud' #自己密钥
data = 'sdadsdsdsfd' #需要加密的内容
AES_Encrypt(key, data)
enctext = AES_Encrypt(key, data)
print(enctext)
text_decrypted = AES_Decrypt(key, enctext)
print(text_decrypted)

名称栏目:Python如何实现AES加密与解密-创新互联
文章起源:http://www.wjwzjz.com/article/dschis.html
在线咨询
服务热线
服务热线:028-86922220
TOP