identity.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. import base64
  2. from Crypto.Cipher import PKCS1_OAEP
  3. from Crypto.PublicKey import RSA
  4. """
  5. RSA解密文件,非发布文件,运行该程序后将客户端的密文输入到该程序回车即可获取用于解密的字符串,将结果输入到客户端即可解密
  6. """
  7. def load_private_key(filename):
  8. # 导入加密的私钥
  9. with open(filename, "rb") as f:
  10. encrypted_key = f.read()
  11. private_key = RSA.import_key(encrypted_key, passphrase="my_password")
  12. return private_key
  13. def get_plaintext(private_key, ciphertext):
  14. """
  15. 获取明文
  16. :param private_key: 私钥
  17. :param ciphertext: base64编码二进制数据
  18. :return:
  19. """
  20. # 创建一个PKCS1_OAEP的解密器
  21. cipher_rsa = PKCS1_OAEP.new(private_key)
  22. # 将Base64编码的加密数据转换回字节
  23. encrypted_data = base64.b64decode(ciphertext)
  24. # 解密数据
  25. decrypted_data = cipher_rsa.decrypt(encrypted_data)
  26. return decrypted_data.decode('utf-8')
  27. ciphertext = input('输入验证信息\n')
  28. result = get_plaintext(load_private_key('private.pem'), ciphertext)
  29. print(result) # 将结果输入到客户端