#!/usr/bin/python3
import base64
import json
import jsonpath
import requests
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5 as Cipher_pkcs1_v1_5
from base64 import b64decode, b64encode
def get_public_key():
"""
备注:加密方式为rsa加密,先获取public_key
:return str
"""
url = "http://ip/publicKey" #获取公钥
response = requests.request("GET", url).text
res = json.loads(response)
_public_key = jsonpath.jsonpath(res, "$..data.publicKey")
return _public_key[0]
def str_to_rsa(text: str):
""""
明文rsa加密
:return:str
"""
_public_key = get_public_key()
__public_key = b64decode(_public_key)
rsa_key = RSA.importKey(__public_key)
cipher = Cipher_pkcs1_v1_5.new(rsa_key) # 创建用于执行pkcs1_v1_5加密或解密的密码
cipher_text = base64.b64encode(cipher.encrypt(text.encode('utf-8')))
_cipher_text = cipher_text.decode('utf-8')
return _cipher_text
if __name__ =="__main__":
_public_key = get_publ
使用Python对数据进行rsa加密
于 2024-02-29 11:26:33 首次发布

最低0.47元/天 解锁文章
1412

被折叠的 条评论
为什么被折叠?



