Windows下python安装PyCrypto加密模块以及使用
2016-12-09 17:59
585 查看
本机环境:
Windows8(Windows系列都可以)python3.4
步骤一:首先下载PyCryto
PyCrypto点击下载—下载完解压假设我解压到H盘,打开cmd,cd到pycrypto-2.6.1文件夹下
执行如下命令:
接着执行:H:\pycrypto-2.6.1>python setup.py install
安装完成!
下面官方对模块的介绍以及使用的一些小例子
This is a collection of both secure hash functions (such as SHA256 andRIPEMD160), and various encryption algorithms (AES, DES, RSA, ElGamal,
etc.). The package is structured to make adding new modules easy.
This section is essentially complete, and the software interface will
almost certainly not change in an incompatible way in the future; all
that remains to be done is to fix any bugs that show up. If you
encounter a bug, please report it in the Launchpad bug tracker at
https://launchpad.net/products/pycrypto/+bugs
from Crypto.Cipher import AES from Crypto.Hash import SHA256 #SHA256例子 hash = SHA256.new() hash.update('你好'.encode('utf-8')) print(hash.digest()) print(hash.hexdigest()) ---------- #AES(对称加密算法) #注意:这里的Key和Block的大小要符合图示的要求 #加密 obj = AES.new('This is a key456',AES.MODE_ECB) message = "The answer is no" ciphertext = obj.encrypt(message) print('AES加密密文:',ciphertext) #解密 obj2 = AES.new('This is a key456',AES.MODE_ECB) detext=obj2.decrypt(ciphertext) print('AES解密密文:',detext.decode()) ---------- #产生随机数的算法 from Crypto import Random rndfile = Random.new() print(rndfile.read(16)) ---------- #ARC4(又称RC4 对称加密算法) from Crypto.Cipher import ARC4 from Crypto.Hash import SHA from Crypto import Random #加密 key = b'Very long and confidential key' nonce = Random.new().read(16) tempkey = SHA.new(key+nonce).digest() cipher = ARC4.new(tempkey) #返回一个ARC4Cipher object msg = cipher.encrypt(b'Open the pod bay doors, HAL') print('ARC4加密密文:',msg) #解密 cipher1 = ARC4.new(tempkey) msg1=cipher1.decrypt(msg) print('ARC4解密密文:',msg1) ---------- #RSA(非对称加密算法) from Crypto.PublicKey import RSA key = RSA.generate(1024) #创建key f = open('mykey.pem','wb') f.write(key.exportKey('PEM'))#导出key,写入文件 f.close() f1= open('publickey.pem','wb') #打开文件 f1.write(key.publickey().exportKey('PEM')) #创建公钥,写入文件 f1.close() f = open('publickey.pem','r') pk = RSA.importKey(f.read()) ciphertext=pk.encrypt(b'helloworld',Random.new().read(16)) print('RSA加密密文:',ciphertext) f1 = open('mykey.pem','r') sk = RSA.importKey(f1.read()) text=sk.decrypt(ciphertext) print('RSA解密密文:',text.decode()) f.close() f1.close() #Attention: # this function performs the plain, primitive RSA encryption (textbook). # In real applications, you always need to use proper cryptographic padding, # and you should not directly encrypt data with this method. # Failure to do so may lead to security vulnerabilities. # It is recommended to use modules Crypto.Cipher.PKCS1_OAEP or Crypto.Cipher.PKCS1_v1_5 instead. #大概意思是这是最简单的RSA加密,但实际应用上是不这样使用的,请参考模块Crypto.Cipher.PKCS1_OAEP or Crypto.Cipher.PKCS1_v1_5 ---------- #Crypto.Cipher.PKCS1_OAEP加密 from Crypto.Cipher import PKCS1_OAEP from Crypto.PublicKey import RSA message = b'To be encrypted' key = RSA.importKey(open('publickey.pem').read()) cipher = PKCS1_OAEP.new(key) ciphertext = cipher.encrypt(message) print('Crypto.Cipher.PKCS1_OAEP加密:',ciphertext) key = RSA.importKey(open('mykey.pem').read()) cipher = PKCS1_OAEP.new(key) message = cipher.decrypt(ciphertext) print('Crypto.Cipher.PKCS1_OAEP解密:',message)
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例
- Python 七步捉虫法