Python基于pycrypto實現(xiàn)的AES加密和解密算法示例
本文實例講述了Python基于pycrypto實現(xiàn)的AES加密和解密算法。分享給大家供大家參考,具體如下:
一 代碼
# -*- coding: UTF-8 -*-
import string
import random
from Crypto.Cipher import AES
def keyGenerater(length):
'''''生成指定長度的秘鑰'''
if length not in (16, 24, 32):
return None
x = string.ascii_letters+string.digits
return ''.join([random.choice(x) for i in range(length)])
def encryptor_decryptor(key, mode):
return AES.new(key, mode, b'0000000000000000')
#使用指定密鑰和模式對給定信息進行加密
def AESencrypt(key, mode, text):
encryptor = encryptor_decryptor(key, mode)
return encryptor.encrypt(text)
#使用指定密鑰和模式對給定信息進行解密
def AESdecrypt(key, mode, text):
decryptor = encryptor_decryptor(key, mode)
return decryptor.decrypt(text)
if __name__ == '__main__':
text = 'Python3.5 is excellent.'
key = keyGenerater(16)
#隨機選擇AES的模式
mode = random.choice((AES.MODE_CBC, AES.MODE_CFB, AES.MODE_ECB, AES.MODE_OFB))
if not key:
print('Something is wrong.')
else:
print('key:', key)
print('mode:', mode)
print('Before encryption:', text)
#明文必須以字節(jié)串形式,且長度為16的倍數(shù)
text_encoded = text.encode()
text_length = len(text_encoded)
padding_length = 16 - text_length%16
text_encoded = text_encoded + b'0'*padding_length
text_encrypted = AESencrypt(key, mode, text_encoded)
print('After encryption:', text_encrypted)
text_decrypted =AESdecrypt(key, mode, text_encrypted)
print('After decryption:', text_decrypted.decode()[:-padding_length])
二 運行結(jié)果
E:\python\python可以這樣學(xué)\第18章 密碼學(xué)編程\code>python AES_test.py
('key:', 'D5pcO6iu0HIbj3I2')
('mode:', 1)
('Before encryption:', 'Python3.5 is excellent.')
('After encryption:', '\xf4\x15\x9f\xaf\xea\xd0\n\x03\xfdf\xf6}9\xaa\xa34\xb4\x1eL2\x0e \x16\xa5 \xff?\x8bA\x8e\xdd\xa8')
('After decryption:', u'Python3.5 is excellent.')
PS:關(guān)于加密解密感興趣的朋友還可以參考本站在線工具:
文字在線加密解密工具(包含AES、DES、RC4等):
http://tools.jb51.net/password/txt_encode
MD5在線加密工具:
http://tools.jb51.net/password/CreateMD5Password
在線散列/哈希算法加密工具:
http://tools.jb51.net/password/hash_encrypt
在線MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:
http://tools.jb51.net/password/hash_md5_sha
在線sha1/sha224/sha256/sha384/sha512加密工具:
http://tools.jb51.net/password/sha_encode
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python加密解密算法與技巧總結(jié)》、《Python編碼操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
Pytorch中關(guān)于F.normalize計算理解
這篇文章主要介紹了Pytorch中關(guān)于F.normalize計算理解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
Python中自然語言處理和文本挖掘的常規(guī)操作詳解
自然語言處理和文本挖掘是數(shù)據(jù)科學(xué)中的重要領(lǐng)域,涉及對文本數(shù)據(jù)的分析和處理,這篇文章為大家介紹了一些常見的任務(wù)和實現(xiàn)方法,需要的可以了解下2025-02-02
Python數(shù)學(xué)建模庫StatsModels統(tǒng)計回歸簡介初識
這篇文章主要為大家介紹了Python數(shù)學(xué)建模庫StatsModels統(tǒng)計回歸的基本概念,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝打擊多多進步2021-10-10
python中range和xrange的區(qū)別(python2和python3)
在Python中,range()?和?xrange()?函數(shù)在早期的Python版本(Python 2)中扮演著不同的角色,但在Python 3中,xrange()?已經(jīng)被移除,并被?range()?取代,下面就來介紹一下,感興趣的可以了解一下2025-04-04

