第一步:下载
pip install pycrypto -i http://mirrors.aliyun.com/pypi/simple/
如果报错的话就把http改成https
第二步:就在你的py文件夹中导入模块
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!') e = '4b2da0787538cddda324761bb70c6e85' d = mycrypt.mydecrypt(e) print e print d注意:实例化MyCrypt的时候需要传入一个秘钥,秘钥的长度为16,24,32字节长度,否则报错