115 lines
2.8 KiB
Python
115 lines
2.8 KiB
Python
#!/usr/bin/env python
|
||
# -*- coding: utf-8 -*-
|
||
"""
|
||
LabVIEW AES解密脚本
|
||
用于解密MES接口返回的加密数据
|
||
|
||
密钥:YaviiMESpassword
|
||
算法:AES-128/ECB/PKCS5Padding
|
||
|
||
依赖安装:
|
||
pip install pycryptodome
|
||
|
||
使用方法:
|
||
python 2025-10-18_硬件融合接口解密脚本.py <encrypted_data>
|
||
|
||
示例:
|
||
python 2025-10-18_硬件融合接口解密脚本.py "U2FsdGVkX1+..."
|
||
|
||
返回:
|
||
{"Object":"root","Class":"password123"}
|
||
"""
|
||
|
||
from Crypto.Cipher import AES
|
||
from Crypto.Util.Padding import unpad
|
||
import base64
|
||
import sys
|
||
import json
|
||
|
||
# AES密钥(必须与Java后端一致,16字节)
|
||
AES_KEY = b"YaviiMESpassword"
|
||
|
||
|
||
def aes_decrypt(encrypted_data):
|
||
"""
|
||
AES解密函数
|
||
|
||
参数:
|
||
encrypted_data: Base64编码的密文字符串
|
||
|
||
返回:
|
||
解密后的明文字符串
|
||
|
||
异常:
|
||
如果解密失败,抛出Exception
|
||
"""
|
||
try:
|
||
# 创建AES解密器(ECB模式)
|
||
cipher = AES.new(AES_KEY, AES.MODE_ECB)
|
||
|
||
# Base64解码
|
||
encrypted_bytes = base64.b64decode(encrypted_data)
|
||
|
||
# AES解密
|
||
decrypted_bytes = cipher.decrypt(encrypted_bytes)
|
||
|
||
# 去除填充(PKCS5/PKCS7)
|
||
decrypted_bytes = unpad(decrypted_bytes, AES.block_size)
|
||
|
||
# 转换为字符串
|
||
return decrypted_bytes.decode('utf-8')
|
||
|
||
except Exception as e:
|
||
raise Exception(f"AES解密失败: {str(e)}")
|
||
|
||
|
||
def main():
|
||
"""
|
||
主函数
|
||
命令行用法:python 2025-10-18_硬件融合接口解密脚本.py <encrypted_data>
|
||
"""
|
||
if len(sys.argv) < 2:
|
||
print("=" * 60)
|
||
print("LabVIEW AES解密工具")
|
||
print("=" * 60)
|
||
print()
|
||
print("用法:")
|
||
print(" python 2025-10-18_硬件融合接口解密脚本.py <encrypted_data>")
|
||
print()
|
||
print("示例:")
|
||
print(" python 2025-10-18_硬件融合接口解密脚本.py \"U2FsdGVkX1+...\"")
|
||
print()
|
||
print("参数说明:")
|
||
print(" encrypted_data: Base64编码的AES密文")
|
||
print()
|
||
print("密钥信息:")
|
||
print(" 算法: AES-128")
|
||
print(" 模式: ECB")
|
||
print(" 密钥: YaviiMESpassword")
|
||
print()
|
||
print("=" * 60)
|
||
sys.exit(1)
|
||
|
||
encrypted_data = sys.argv[1]
|
||
|
||
try:
|
||
# 解密
|
||
decrypted = aes_decrypt(encrypted_data)
|
||
|
||
# 尝试格式化JSON输出(美化)
|
||
try:
|
||
json_obj = json.loads(decrypted)
|
||
print(json.dumps(json_obj, ensure_ascii=False, indent=2))
|
||
except:
|
||
# 如果不是JSON,直接输出
|
||
print(decrypted)
|
||
|
||
except Exception as e:
|
||
print(f"错误: {str(e)}", file=sys.stderr)
|
||
sys.exit(1)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|
||
|