import base64
from Crypto.Cipher import PKCS1_OAEP
from Crypto.PublicKey import RSA
"""
RSA解密文件，非发布文件，运行该程序后将客户端的密文输入到该程序回车即可获取用于解密的字符串，将结果输入到客户端即可解密
"""

def load_private_key(filename):
    # 导入加密的私钥
    with open(filename, "rb") as f:
        encrypted_key = f.read()
        private_key = RSA.import_key(encrypted_key, passphrase="my_password")
    return private_key


def get_plaintext(private_key, ciphertext):
    """
    获取明文
    :param private_key: 私钥
    :param ciphertext: base64编码二进制数据
    :return:
    """
    # 创建一个PKCS1_OAEP的解密器
    cipher_rsa = PKCS1_OAEP.new(private_key)
    # 将Base64编码的加密数据转换回字节
    encrypted_data = base64.b64decode(ciphertext)
    # 解密数据
    decrypted_data = cipher_rsa.decrypt(encrypted_data)
    return decrypted_data.decode('utf-8')


ciphertext = input('输入验证信息\n')
result = get_plaintext(load_private_key('private.pem'), ciphertext)
print(result)  # 将结果输入到客户端
