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') #使用指定密鑰和模式對給定信息進(jìn)行加密 def AESencrypt(key, mode, text): encryptor = encryptor_decryptor(key, mode) return encryptor.encrypt(text) #使用指定密鑰和模式對給定信息進(jìn)行解密 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) #隨機(jī)選擇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])
二 運(yùn)行結(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ìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
Python3中configparser模塊讀寫ini文件并解析配置的用法詳解
這篇文章主要介紹了Python3中configparser模塊讀寫ini文件并解析配置的用法詳解,需要的朋友可以參考下2020-02-02pytorch網(wǎng)絡(luò)模型構(gòu)建場景的問題介紹
這篇文章主要介紹了pytorch網(wǎng)絡(luò)模型構(gòu)建場景的注意點(diǎn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-03-03Python使用requests發(fā)送POST請求實例代碼
這篇文章主要介紹了Python使用requests發(fā)送POST請求實例代碼,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-01-01python實現(xiàn)scrapy爬蟲每天定時抓取數(shù)據(jù)的示例代碼
這篇文章主要介紹了python實現(xiàn)scrapy爬蟲每天定時抓取數(shù)據(jù)的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01DjangoWeb使用Datatable進(jìn)行后端分頁的實現(xiàn)
這篇文章主要介紹了DjangoWeb使用Datatable進(jìn)行后端分頁的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05