95 lines
3.3 KiB
Python
95 lines
3.3 KiB
Python
import requests
|
||
import json
|
||
from Crypto.Cipher import DES
|
||
from Crypto.Util.Padding import pad
|
||
import base64
|
||
|
||
# === 配置你的密钥和 IV(必须是 8 字节!)===
|
||
DEFAULT_KEY = "12345678" # 替换为实际 key,必须 8 字符
|
||
DEFAULT_IV = "abcdefgh" # 替换为实际 iv,必须 8 字符
|
||
|
||
def get_des_encrypt(data: str, _key: str = DEFAULT_KEY, _iv: str = DEFAULT_IV) -> str:
|
||
"""
|
||
使用 DES/CBC/PKCS7 加密字符串,与 CryptoJS.DES.encrypt 兼容。
|
||
"""
|
||
key = _key.encode('utf-8')
|
||
iv = _iv.encode('utf-8')
|
||
|
||
if len(key) != 8:
|
||
raise ValueError("DES key must be exactly 8 bytes long")
|
||
if len(iv) != 8:
|
||
raise ValueError("DES IV must be exactly 8 bytes long")
|
||
|
||
plaintext = data.encode('utf-8')
|
||
padded_data = pad(plaintext, DES.block_size)
|
||
cipher = DES.new(key, DES.MODE_CBC, iv)
|
||
encrypted_bytes = cipher.encrypt(padded_data)
|
||
return base64.b64encode(encrypted_bytes).decode('utf-8')
|
||
|
||
|
||
def send_unencrypted_request(url, data):
|
||
headers = {
|
||
"Content-Type": "application/json"
|
||
}
|
||
try:
|
||
response = requests.post(url, headers=headers, data=json.dumps(data))
|
||
print("🔓 [未加密] Status Code:", response.status_code)
|
||
print("🔓 [未加密] Response Text:", response.text)
|
||
result = response.json()
|
||
token = result.get("data", {}).get("token")
|
||
if token:
|
||
print("✅ [未加密] 登录成功,Token:", token)
|
||
else:
|
||
print("❌ [未加密] 未获取到 Token,响应内容:", result)
|
||
except requests.RequestException as e:
|
||
print("🌐 [未加密] 请求异常:", e)
|
||
except json.JSONDecodeError:
|
||
print("🧩 [未加密] 响应不是合法的 JSON 格式")
|
||
|
||
|
||
def send_encrypted_request(url, data):
|
||
# 将原始数据转为紧凑 JSON 字符串
|
||
data_str = json.dumps(data, separators=(',', ':'))
|
||
# 加密
|
||
encrypted_data = get_des_encrypt(data_str, DEFAULT_KEY, DEFAULT_IV)
|
||
# 构造最终 payload
|
||
payload = {
|
||
"encType": 0,
|
||
"x_flag": "",
|
||
"data": encrypted_data
|
||
}
|
||
headers = {
|
||
"Content-Type": "application/json"
|
||
}
|
||
try:
|
||
response = requests.post(url, headers=headers, data=json.dumps(payload))
|
||
print("🔒 [已加密] Status Code:", response.status_code)
|
||
print("🔒 [已加密] Response Text:", response.text)
|
||
result = response.json()
|
||
token = result.get("data", {}).get("token")
|
||
if token:
|
||
print("✅ [已加密] 登录成功,Token:", token)
|
||
else:
|
||
print("❌ [已加密] 未获取到 Token,响应内容:", result)
|
||
except requests.RequestException as e:
|
||
print("🌐 [已加密] 请求异常:", e)
|
||
except json.JSONDecodeError:
|
||
print("🧩 [已加密] 响应不是合法的 JSON 格式")
|
||
except ValueError as ve:
|
||
print("⚠️ [已加密] 加密参数错误:", ve)
|
||
|
||
|
||
if __name__ == '__main__':
|
||
url = "http://127.0.0.1:8084/c/v1/wellMudLogAndWireLine/system/privateManager/userLoginAggregate/other/login"
|
||
login_data = {
|
||
"userId": "admin",
|
||
"password": "123456"
|
||
}
|
||
|
||
# ========== 选择模式 ==========
|
||
USE_ENCRYPTION = False # 改为 False 则发送未加密请求
|
||
|
||
if USE_ENCRYPTION:
|
||
send_encrypted_request(url, login_data)
|
||
else:
|
||
send_unencrypted_request(url, login_data) |