本文實例為大家分享了python實現(xiàn)AES加密和解密的具體代碼,供大家參考,具體內(nèi)容如下

參考:python實現(xiàn)AES加密和解密
AES加密算法是一種對稱加密算法, 他有一個密匙, 即用來加密, 也用來解密
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類型的數(shù)據(jù)
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)
# 將加密數(shù)據(jù)轉(zhuǎn)換位bytes類型數(shù)據(jù)
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)
網(wǎng)頁題目:python簡單實現(xiàn)AES加密和解密-創(chuàng)新互聯(lián)
網(wǎng)站網(wǎng)址:http://chinadenli.net/article30/dghipo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、品牌網(wǎng)站建設(shè)、云服務(wù)器、搜索引擎優(yōu)化、小程序開發(fā)、外貿(mào)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)