本文主要探索的是python的Crypto模塊實現AES加密,分享了具體實現代碼,下面看看具體內容。
學了使用Crypto模塊的AES來加密文件,現在記錄下來便于后邊兒查看。
在剛開始知道這個模塊的時候,連基本的Crypto模塊的安裝都花了很多很多時間來搞,也不知道什么情況反正是折騰很久了才安裝起的,記得是包安裝起來了,但使用的時候始終提示找不到Crypto.Cipher模塊。然后怎么解決的呢?
一、把我的python換成了64位的,本來電腦就是64位的也不知道之前是啥情況安裝成32位的了。(O(∩_∩)O哈哈~)
二、安裝了VCForPython27.msi
三、在cmd中執行:
pip install pycrypto -i http://mirrors.aliyun.com/pypi/simple/
經過上邊兒的幾個步驟,我是能夠成功執行
from Crypto.Cipher import AES
現在上一個實例代碼:
# !/usr/bin/env python
# coding: utf-8
'''
'''
from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex
class MyCrypt():
def __init__(self, key):
self.key = key
self.mode = AES.MODE_CBC
def myencrypt(self, text):
length = 16
count = len(text)
print count
if count < length:
add = length - count
text= text + ('\0' * add)
elif count > length:
add = (length -(count % length))
text= text + ('\0' * add)
# print len(text)
cryptor = AES.new(self.key, self.mode, b'0000000000000000')
self.ciphertext = cryptor.encrypt(text)
return b2a_hex(self.ciphertext)
def mydecrypt(self, text):
cryptor = AES.new(self.key, self.mode, b'0000000000000000')
plain_text = cryptor.decrypt(a2b_hex(text))
return plain_text.rstrip('\0')
if __name__ == '__main__':
mycrypt = MyCrypt('abcdefghjklmnopq')
e = mycrypt.myencrypt('hello,world!')
d = mycrypt.mydecrypt(e)
print e
print d
在cmd中執行結果:

總結
以上就是本文關于python的Crypto模塊實現AES加密實例代碼的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。