使用python的Crypto模块的AES加密文件
2015-05-23 17:28
218 查看
[code]学了使用Crypto模块的AES来加密文件,现在记录下来便于后边儿查看。
在刚开始知道这个模块的时候,连基本的Crypto模块的安装都花了很多很多时间来搞,也不知道什么情况反正是折腾很久了才安装起的,记得是包安装起来了,但使用的时候始终提示找不到Crypto.Cipher模块。然后怎么解决的呢?
一、把我的python换成了64位的,本来电脑就是64位的也不知道之前是啥情况安装成32位的了。(O(∩_∩)O哈哈~)
二、安装了VCForPython27.msi
三、在cmd中执行:
[code]pip install pycrypto -i http://mirrors.aliyun.com/pypi/simple/[/code]
经过上边儿的几个步骤,我是能够成功执行[code]from Crypto.Cipher import AES
现在上一个实例代码:[code]# !/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 AES加密模块使用
- Python写的AES文件加密模块
- python AES加密模块使用
- python的Crypto模块实现AES加密实例代码
- python 使用poster模块进行http方式的文件传输到服务器
- python---文件保存xls格式,xlwt,xlrd的模块使用
- 【python】如何批量读取文件夹的所有文件数据,os模块与open函数结合使用实例
- 重温python之OS模块的简单使用4:os.chmod() 更改文件或目录的权限
- 重温python之OS模块的简单使用5:os.chown()更改文件所有者
- python文件基础(3)--pickle 模块的使用
- python文件基础(3)--pickle 模块的使用
- 使用Python进行AES加密和解密
- 解决python模块调用时代码中使用相对路径访问的文件,提示文件不存在的问题
- 使用Python实现AES加密解密
- 使用Crypto进行AES的cbc模式加密
- Python使用PyCrypto实现AES加密功能示例
- Python3.6使用logger模块,2次print,重定向,输出信息到指定文件,使用traceback模块重定向错误输出
- Robot Framework实现使用PyCrypto调用AES加密文件
- Windows下python安装PyCrypto加密模块以及使用
- 使用crypto模块实现md5加密功能(解决中文加密前后端不一致的问题)