重构结构

This commit is contained in:
limoruirui
2022-08-27 11:32:29 +08:00
parent 3cc4a67d38
commit 48f9205f25
18 changed files with 222 additions and 106882 deletions

23
tools/rsa_encrypt.py Normal file
View File

@@ -0,0 +1,23 @@
#!/usr/bin/python3
# -- coding: utf-8 --
# -------------------------------
# @Author : github@limoruirui https://github.com/limoruirui
# @Time : 2022/8/23 13:05
# -------------------------------
from Crypto.PublicKey.RSA import importKey
from Crypto.Cipher import PKCS1_v1_5
from base64 import b64encode
class RSA_Encrypt:
def __init__(self, key):
if isinstance(key, str):
# 若提供的rsa公钥不为pem格式 则先将hex转化为pem格式
self.key = bytes.fromhex(key) if "PUBLIC KEY" not in key else key.encode()
else:
print("提供的公钥格式不正确")
def Encrypt(self, data, b64=False):
pub_key = importKey(self.key)
cipher = PKCS1_v1_5.new(pub_key)
rsa_text = cipher.encrypt(data.encode("utf8"))
rsa_text = b64encode(rsa_text).decode() if b64 else rsa_text.hex()
return rsa_text