python 实现RSA公钥加密,私钥解密
2021-01-03 16:27
标签:code def mes err ext 校验 import base64 公钥 python 实现RSA公钥加密,私钥解密 标签:code def mes err ext 校验 import base64 公钥 原文地址:https://www.cnblogs.com/mian-1122/p/12991024.htmlfrom Crypto.PublicKey import RSA
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
# 私钥
private_key = ‘‘‘-----BEGIN RSA PRIVATE KEY-----
5353dfggd
-----END RSA PRIVATE KEY-----
‘‘‘
# 公钥
public_key = ‘‘‘-----BEGIN PUBLIC KEY-----
hfgghftetet
-----END PUBLIC KEY-----‘‘‘
def rsa_encrypt(message):
"""校验RSA加密 使用公钥进行加密"""
cipher = Cipher_pkcs1_v1_5.new(RSA.importKey(public_key))
cipher_text = base64.b64encode(cipher.encrypt(message.encode())).decode()
return cipher_text
def rsa_decrypt(text):
"""校验RSA加密 使用私钥进行解密"""
cipher = Cipher_pkcs1_v1_5.new(RSA.importKey(private_key))
retval = cipher.decrypt(base64.b64decode(text), ‘ERROR‘).decode(‘utf-8‘)
return retval
文章标题:python 实现RSA公钥加密,私钥解密
文章链接:http://soscw.com/index.php/essay/39851.html