test_asymmetric_encryption.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. from utils.asymmetric_encryption import AsymmetricEncryption
  2. import os
  3. def test_asymmetric_encryption():
  4. """测试非对称加密功能"""
  5. print("开始测试非对称加密功能...")
  6. # 测试1:生成密钥对
  7. print("\n1. 生成密钥对测试...")
  8. private_pem, public_pem = AsymmetricEncryption.generate_key_pair()
  9. print(f"私钥长度: {len(private_pem)} 字节")
  10. print(f"公钥长度: {len(public_pem)} 字节")
  11. print(f"私钥前100字节: {private_pem[:100]!r}")
  12. print(f"公钥前100字节: {public_pem[:100]!r}")
  13. # 测试2:加密解密测试
  14. print("\n2. 加密解密测试...")
  15. test_message = "这是一个测试消息,用于测试非对称加密功能!"
  16. print(f"原始消息: {test_message}")
  17. # 加密
  18. encrypted = AsymmetricEncryption.encrypt(test_message, public_pem)
  19. print(f"加密后: {encrypted}")
  20. # 解密
  21. decrypted = AsymmetricEncryption.decrypt(encrypted, private_pem)
  22. print(f"解密后: {decrypted}")
  23. # 验证
  24. assert decrypted == test_message, "解密失败,结果与原始消息不符!"
  25. print("✓ 加密解密测试通过!")
  26. # 测试3:密钥文件保存和加载测试
  27. print("\n3. 密钥文件保存和加载测试...")
  28. private_key_path = "private_key.pem"
  29. public_key_path = "public_key.pem"
  30. # 保存密钥
  31. AsymmetricEncryption.save_key_to_file(private_pem, private_key_path)
  32. AsymmetricEncryption.save_key_to_file(public_pem, public_key_path)
  33. print(f"✓ 密钥已保存到文件: {private_key_path}, {public_key_path}")
  34. # # 加载密钥
  35. # loaded_private_pem = AsymmetricEncryption.load_key_from_file(private_key_path)
  36. # loaded_public_pem = AsymmetricEncryption.load_key_from_file(public_key_path)
  37. # print("✓ 密钥已从文件加载")
  38. # # 验证加载的密钥是否正确
  39. # encrypted2 = AsymmetricEncryption.encrypt(test_message, loaded_public_pem)
  40. # decrypted2 = AsymmetricEncryption.decrypt(encrypted2, loaded_private_pem)
  41. # assert decrypted2 == test_message, "使用加载的密钥解密失败!"
  42. # print("✓ 使用加载的密钥加密解密测试通过!")
  43. # # 测试4:生成密钥对并保存测试
  44. # print("\n4. 生成密钥对并保存测试...")
  45. # private_key_path2 = "test_private_key2.pem"
  46. # public_key_path2 = "test_public_key2.pem"
  47. # AsymmetricEncryption.generate_key_pair_and_save(private_key_path2, public_key_path2)
  48. # print(f"✓ 密钥对已生成并保存到文件: {private_key_path2}, {public_key_path2}")
  49. # # 验证生成并保存的密钥
  50. # loaded_private_pem2 = AsymmetricEncryption.load_key_from_file(private_key_path2)
  51. # loaded_public_pem2 = AsymmetricEncryption.load_key_from_file(public_key_path2)
  52. # encrypted3 = AsymmetricEncryption.encrypt(test_message, loaded_public_pem2)
  53. # decrypted3 = AsymmetricEncryption.decrypt(encrypted3, loaded_private_pem2)
  54. # assert decrypted3 == test_message, "使用生成并保存的密钥解密失败!"
  55. # print("✓ 使用生成并保存的密钥加密解密测试通过!")
  56. # # 清理临时文件
  57. # print("\n5. 清理临时文件...")
  58. # for file_path in [private_key_path, public_key_path, private_key_path2, public_key_path2]:
  59. # if os.path.exists(file_path):
  60. # os.remove(file_path)
  61. # print(f"✓ 删除临时文件: {file_path}")
  62. # print("\n所有测试通过!非对称加密功能正常工作。")
  63. if __name__ == "__main__":
  64. test_asymmetric_encryption()