非对称加密的应用

第一步获取非对称秘钥:

可以通过网站获取:在线RSA加密解密,RSA公钥私钥加密解密-ME2在线工具

第二种,代码生成:利用Python Crypto生成RSA公钥私钥 - it610.com

第二步:对应的加解密:

from 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 PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC2LlOlPEUU6R/I
qTjeoBGfDTKbQu93ZAjlp0phNq9b2CwDLxO79Tb6Ns0kWYR/UK/zLE2UoxgTd/Ky
fQvbTrS9VHpSwFAycYZAOtkQOnBsyfHhiFiDl3jN52lNvmE8sq7j5qas+kbyTLf8
+7wEUxKSgSCkIueZ46gK9wLG8NHEIpbo6dJQ5FYsc4Fz66hWQPFHm9o3tTMROGyh
JK/TXNbqGgJDEfHswXtB+yPS4u8/xdOei8OFts8O9sAjvhtRaL9FAM3Ow54yLhuG
2AdTm78/I+cKcDv+tmi2mPm1OpFCum8v6x8HfdoKyAK3hEINmZXg0Xzz+Yy4M4jF
VsR/A+ylAgMBAAECggEBAIiMJpOlnd2zcegztfVfInYFLVYkEFp7NxPylJRSqZL8
btigE3mArQlTw+Sn1+CZOJfaL/flWmlVMSpJAXcKAfwacmjNzg5Q+p/vu7wnIRbZ
Mhup1zYidSBXf50OwdD92Inm2iyyljSL/id3AxB2t2fpsu5zXwYAFXbKd2FnJMKN
KXzJjrd1mu/LyMWrbcbeCyM2HGNJ3700yD58TQKBcn5ueo1NlW6w/xsP4SZMPNWV
3fnrOng+G43pA0hT7PRdH1SNsVIxhgKuOJ4hylE7zLX1a+dH6SisaeMMs4B6Sb3u
Y+Z5E4yaUeJtSUAL7A44kFfypw/Is1eHvMZ+qB/BXWECgYEA64tdejZuqtHnzs4o
kDn50ysWspVJ7sYeXCj6TcxV8Dcj6fZhBpsL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S
koEgpCLnmeOoCvcCxvDRxCKW6OnSUORWLHOBc+uoVkDxR5vaN7UzEThsoSSv01zW
6hoCQxHx7MF7Qfsj0uLvP8XTnovDhbbPDvbAI74bUWi/RQDNzsOeMi4bhtgHU5u/
PyPnCnA7/rZotpj5tTqRQrpvL+sfB33aCsgCt4RCDZmV4NF88/mMuDOIxVbEfwPs
pQIDAQAB
-----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()))
    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
if __name__ == '__main__':
    enc_result = rsa_encrypt("hello,你好")
    dec_result = rsa_decrypt(enc_result)
    print(dec_result)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

会发paper的学渣

您的鼓励和将是我前进的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值