安装:
pip install pycryptodome
举例1、
#生成RSA公钥和私钥
from Crypto import Random
from Crypto.PublicKey import RSA
random_generator=Random.new().read
rsa=RSA.generate(2048,random_generator)
'''
generate(bits, randfunc=None, e=65537)
参数解释:
bits: 字节大小,一般为1024的整数倍
randfunc: 随机函数,默认为Crypto.Random.get_random_bytes
e=65537: 是公共RSA指数,必须为整数,一般保持默认
'''
#随机值生成私钥
private_key=rsa.exportKey()
print(private_key.decode('utf-8'))
print('###########################')
#随机值生成公钥
public_key=rsa.publickey().exportKey()
print(public_key.decode('utf-8'))
举例2、
#使用公钥加密,使用私钥解密
from Crypto import Random
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5 as PKCS1_cipher
import base64
random_generator = Random.new().read
rsa = RSA.generate(2048,random_generator)
#私钥
private_key = rsa.exportKey()
with open("private_a.rsa","wb") as f:
f.write(private_key)
#公钥
public_key = rsa.publickey().exportKey()
with open("public_a.rsa","wb") as f:
f.write(public_key)
#使用公钥对内容进行加密
message = "需要加密的信息"
with open("public_a.rsa","r") as f:
key=f.read()
pub_key = RSA.importKey(str(key))
cipher = PKCS1_cipher.new(pub_key)
rsa_text = base64.b64encode(cipher.encrypt(bytes(message.encode("utf-8)"))))
print(rsa_text.decode("utf-8"))
#使用私钥对内容进行解密
with open("private_a.rsa","r") as f:
key = f.read()
pri_Key = RSA.importKey(str(key))
cipher = PKCS1_cipher.new(pri_Key)
back_text = cipher.decrypt(base64.b64decode(rsa_text),0)
print(back_text.decode("utf-8"))
"""
结果:
UxPUmbAwFEfvL12pbCljv63Gh883lJKNPPV0ukIY4XWf4U8fIC3PpYOWavkIHKn53NdTQ
19fQ6XwWnUXwV7nrgtAQdv3MjRnOW0NJ2//cf5QC6VoaobyDhNm2ETvOp+JfbS0UQftk
xuhc5TTplLQW8U/2ieJXH0pAtuUE9TL9qYY14YmJT3jrz8WNBdbkh2H5tdr8pUB/+AWnP
Jjcu5AUDOOmlzl6imnkqmF7ztFlPxy8WCLDvxmSH8Z2i/y+N/8CFlBonHvfsHdXD2HIzCH
1BTRbVasr2oPJ+dUF3mrvw16oY1F2nH/LhIDgrkpW7XJzfHhWDbYZ8Kry2R8/xAFhw==
需要加密的信息
"""
举例3、
#私钥生成签名,公钥验证签名
from Crypto.PublicKey import RSA
from Crypto.Hash import SHA
from Crypto.Signature import PKCS1_v1_5 as PKCS1_signature
import base64
message = "需要加密的信息"
#使用私钥生成签名
with open("private_a.rsa","r") as f:
key = f.read()
pri_Key = RSA.importKey(str(key))
signer = PKCS1_signature.new(pri_Key)
digest = SHA.new()
digest.update(message.encode("utf-8"))
sign = signer.sign(digest)
signature = base64.b64encode(sign)
print(signature.decode("utf-8"))
#使用公钥验证签名
with open("public_a.rsa","r") as f:
key=f.read()
pub_key = RSA.importKey(str(key))
verifier = PKCS1_signature.new(pub_key)
digest = SHA.new()
digest.update(message.encode("utf-8"))
print(verifier.verify(digest,base64.b64decode(signature)))
"""
结果:
BZhqDtSVZPFpLkdoG4HOcQxYLp8OdJrniW49P1jcPK14A7kdJjiWOwNpcboK8NgQtwkQ
jC4QRXSvdiPS2SecxUV7iy1Qd7zSWFyfEFNmq5n2Z3R/HsvmA3xsyb9QMTJZQbf/agPW
ZNpOTH/IWVQNLqU5LtfIZ6VWuQFO031Cm4AkqgHIx9Su0aI8kT8ysao5Z+l7C4PgYZuz
YJgJHkUe6gFRDL/UHasPjXHFOP5+VCMauDv1GPrugnpzBoqtVGm7WXwEpI7SuOxQM
Zx/o+HeWgodu3DXw6KbWPtq0LEDMPxD0UhvInKSNh09JLzzA4pcYQp6ydaV4z7IHlBrJ
vXkSA==
True
"""
举例4、
#AES加密和解密
from Crypto.Cipher import AES
from binascii import b2a_hex,a2b_hex
message = "需要加密的信息"
key="aes_keysaes_keyseas_keys"
mode = AES.MODE_OFB
cryptor = AES.new(key.encode("utf-8"),mode,b'0000000000000000')
"""
参数解释:
key.encode("utf-8"):为加密和解密时使用的秘钥,长度有限制,一般为16,24,32
mode=AES.MODE_OFB : 为AES的不同模式
b'0000000000000000':为表示16进制
"""
length = 16
count = len(message)
if count % length !=0:
add = length - (count % length)
else:
add = 0
message = message + ('\0' * add)
#AES加密
ciphertext = cryptor.encrypt(message.encode("utf-8"))
result = b2a_hex(ciphertext) #对加密结果进行16进制处理
print(result.decode("utf-8"))
#AES解密
cryptor = AES.new(key.encode("utf-8"),mode,b'0000000000000000')
plain_text = cryptor.decrypt(a2b_hex(result)) #对解密结果进行16进制处理
print(plain_text.decode("utf-8").rstrip('\0'))
"""
结果:
b6cf5298b530c778575c419e0c48c27f354c0601b6931a61b6ed5a794874
需要加密的信息
"""
python的Crypto模块
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 最近代码里用到DES加解密的程序,为了在本地也能正常调试开发。所以需要在现在的MAC下安装 crypto 扩展,主...
- 前言 安装Cryto模块用pip3 install pycrypto老是会报错。搞了半天,最终决定在linux下面...
- # 先导入所需要的包 pip3 install Crypto # 再安装pycrypto pip3 install...
- from Crypto import Random ImportError: No module named Cr...
- 1.pip install crypto; 会安装很多包:chardet, certifi, urllib3, i...