python rsa 加解密
import base64
import os
from Crypto import Random
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5
__all__ = ["CookieRsa"]
class CookieRsa:
def __init__(self):
self.__config_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "config")
def __init_key(self):
rsa = RSA.generate(1024, Random.new().read)
private_key = rsa.exportKey()
private_key_path = os.path.join(self.__config_path, "private_key.pem")
public_key = rsa.publickey().exportKey()
public_key_path = os.path.join(self.__config_path, "public_key.pem")
with open(private_key_path, 'w') as f:
f.write(private_key.decode())
with open(public_key_path, 'w') as f:
f.write(public_key.decode())
return private_key, public_key
def __get_private_key(self):
private_key_path = os.path.join(self.__config_path, "private_key.pem")
if os.path.exists(private_key_path):
with open(private_key_path, "r") as f:
private_key = f.read()
else:
private_key = self.__init_key()[0]
return private_key
def __get_public_key(self):
public_key_path = os.path.join(self.__config_path, "public_key.pem")
if os.path.exists(public_key_path):
with open(public_key_path, "r") as f:
public_key = f.read()
else:
public_key = self.__init_key()[1]
return public_key
def rsa_encrypt(self, content: str) -> str:
public = RSA.importKey(self.__get_public_key())
cipher = PKCS1_v1_5.new(public)
return base64.b64encode(cipher.encrypt(content.encode('utf-8'))).decode('utf-8')
def rsa_decrypt(self, content: str) -> str:
private = RSA.importKey(self.__get_private_key())
cipher = PKCS1_v1_5.new(private)
return cipher.decrypt(base64.b64decode(content), b'error: decrypt fail').decode('utf-8')