您的位置:首页 > 编程语言 > Python开发

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 and

RIPEMD160), 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