# -*- coding: utf-8 -*-
from Crypto import Random
from Crypto.PublicKey import RSA
from Crypto.Hash import SHA
from Crypto.Cipher import PKCS1_v1_5 as cipher_PKCS1_v1_5
from Crypto.Signature import PKCS1_v1_5 as signature_PKCS1_v1_5
import base64
def get_key():
rsa = RSA.generate(1024, Random.new().read)
private_pem = rsa.exportKey()
public_pem = rsa.publickey().exportKey()
return {
"public_key": public_pem.decode(),
"private_key": private_pem.decode()
}
# 公钥加密
def rsa_encode(message, public_key):
rsakey = RSA.importKey(public_key)
cipher = cipher_PKCS1_v1_5.new(rsakey)
cipher_text = base64.b64encode(
cipher.encrypt(message.encode(encoding="utf-8")))
return cipher_text.decode()
# 私钥解密
def rsa_decode(cipher_text, private_key):
rsakey = RSA.importKey(private_key)
cipher = cipher_PKCS1_v1_5.new(rsakey)
text = cipher.decrypt(base64.b64decode(cipher_text), "ERROR")
if text == "ERROR":
return text
return text.decode()
def rsa_sinature_encode(message, private_key):
rsakey = RSA.importKey(private_key)
signer = signature_PKCS1_v1_5.new(rsakey)
digest = SHA.new()
digest.update(message.encode("utf8"))
sign = signer.sign(digest)
signature = base64.b64encode(sign)
return signature
pass
def rsa_signature_decode(message, public_key, signature):
rsakey = RSA.importKey(public_key)
signer = signature_PKCS1_v1_5.new(rsakey)
digest = SHA.new()
digest.update(message.encode("utf8"))
is_verify = signer.verify(digest, base64.b64decode(signature))
return is_verify
pass
private_key = """-----BEGIN RSA PRIVATE KEY-----
MIICXgIBAAKBgQDBZqKgS7Zl1qBA8vRGwZWiExJdPgHVR+RYowmlM8sylY2aZtNc
mfEqFtvOtzFTHMICSYm4dOiIm0CWPejWQJ+slMiICaR7jRQtxXvOu8UKV+hvIlX6
ckVHD7bJSZHlzmK/oEbE+D2g8rDY2V4i4obysQ0CA0auD5vP7lX1I0hBQQIDAQAB
AoGAPiU/zqhllPXRiu1G51GfRnRAa2vYZPN0AbNqDwbEnZ2G4f12n4PsBa1Qtm0R
UXalNNzyfy455mOU7nI4VlhvthImRr3js4DS0+Jh8fygzv9DfLDqR4rzjVqqa4Ux
pE/6SFDQ8DTmzJuWu9Zoij7k+ykC70DszMbxlrHLjPBpL30CQQDGIcUVwv0E6/+G
aXzRYs80biB7DQwyQfRx2nbC2FI23IBsErjh6maU25/jEZ9wZIDcFCrfySSDbelO
3JbBR3oDAkEA+eMhk1FaGuqNOJ5eVRUOGTleZBjlPUnvYcZRMZq3toQLBAAaXFVT
KSSryfwH2OOh3JSrR8OiX7gcSdeMBKQWawJBAIrHwrXQg8v7xlsO7pf3vy15ahTm
+cEGQ8OwA3nNbiDxaJ7KqmfL+RiRkbfKc0SVkVEWa0WfLY1F4qMTjiFKAKMCQQCt
Ry/wHNhzk7Prz/2v2BLXsO1gUdccodlKHqAM2rGL8ZJF6uBcqJorDI/HV2oFnwuN
jtYaxHEjGOMc3Uftq+8TAkEAnUottGXSa33csSNQJ1pIb561KdWSNaVgC/wtARPC
nxuGPIBmRqgQmiRZ+08AnicudzBF8Os7C42Y4OkeMbEP0A==
-----END RSA PRIVATE KEY-----
"""
public_key = """-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDBZqKgS7Zl1qBA8vRGwZWiExJd
PgHVR+RYowmlM8sylY2aZtNcmfEqFtvOtzFTHMICSYm4dOiIm0CWPejWQJ+slMiI
CaR7jRQtxXvOu8UKV+hvIlX6ckVHD7bJSZHlzmK/oEbE+D2g8rDY2V4i4obysQ0C
A0auD5vP7lX1I0hBQQIDAQAB
-----END PUBLIC KEY-----
"""
if __name__ == '__main__':
# keys = get_key()
# print(keys["public_key"])
# print(keys["private_key"])
#加密解密
message = "hello world!!!!!"
encode_msg = rsa_encode(message, public_key)
print("encode_msg ==== ", encode_msg)
message = rsa_decode(encode_msg, private_key)
print("message ==== ", message)
#签名验证
message = "it's me"
signature = rsa_sinature_encode(message, private_key)
print("signature ==== ", signature)
verify = rsa_signature_decode(message, public_key, signature)
print("verify ==== ", verify)
转载:https://blog.csdn.net/chinabhlt/article/details/101226866
查看评论