python 实现RSA公钥加密,私钥解密
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 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
相关推荐
rein0 2020-01-29
wuxiaosi0 2019-07-01
scuyxi 2015-11-04
逍遥友 2020-06-02
taiyangshenniao 2019-12-26
hulao 2020-06-12
STchaoL 2020-06-04
dailinqing 2020-05-26
yuanran0 2020-05-09
LULUBAO 2020-05-05
happyfreeangel 2020-04-26
BraveWangDev 2020-03-08
magicandy 2020-01-02
mmyCSDN 2020-01-06
ConquerALL 2013-06-08