feat(login): 实现DES加密登录功能- 添加 DES/CBC/PKCS7 加密函数,兼容 CryptoJS.DES.encrypt
- 使用默认密钥和 IV 对登录数据进行加密 - 修改请求 payload 结构,将加密后的字符串作为 data 字段值 - 更新接口地址并调整请求逻辑 - 增强错误处理与调试信息打印- 优化响应解析方式,正确提取 token 信息main
parent
5fa68924e3
commit
3ab5f15b0e
|
|
@ -1,17 +1,49 @@
|
||||||
import requests
|
import requests
|
||||||
import json
|
import json
|
||||||
|
from Crypto.Cipher import DES
|
||||||
|
from Crypto.Util.Padding import pad
|
||||||
|
import base64
|
||||||
|
|
||||||
# 接口地址
|
# === 配置你的密钥和 IV(必须是 8 字节!)===
|
||||||
url = "http://127.0.0.1:8084/c/v1/wellMudLogAndWireLine/system/privateManager/userLoginAggregate/other/login"
|
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:
|
||||||
payload = {
|
"""
|
||||||
"encType": 0,
|
使用 DES/CBC/PKCS7 加密字符串,与 CryptoJS.DES.encrypt 兼容。
|
||||||
"x_flag": "",
|
"""
|
||||||
"data": {
|
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')
|
||||||
|
|
||||||
|
|
||||||
|
# === 构造原始登录数据 ===
|
||||||
|
login_data = {
|
||||||
"userId": "admin",
|
"userId": "admin",
|
||||||
"password": "123456"
|
"password": "123456"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# 转为 JSON 字符串(注意:不要加空格,确保与前端一致)
|
||||||
|
data_str = json.dumps(login_data, separators=(',', ':')) # 紧凑格式,无空格
|
||||||
|
|
||||||
|
# 加密
|
||||||
|
encrypted_data = get_des_encrypt(data_str, DEFAULT_KEY, DEFAULT_IV)
|
||||||
|
|
||||||
|
# === 构造最终请求 payload ===
|
||||||
|
payload = {
|
||||||
|
"encType": 0,
|
||||||
|
"x_flag": "",
|
||||||
|
"data": encrypted_data # 注意:这里现在是加密后的字符串,不是 dict!
|
||||||
}
|
}
|
||||||
|
|
||||||
# 请求头
|
# 请求头
|
||||||
|
|
@ -19,25 +51,31 @@ headers = {
|
||||||
"Content-Type": "application/json"
|
"Content-Type": "application/json"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# 接口地址
|
||||||
|
url = "http://127.0.0.1:8084/c/v1/wellMudLogAndWireLine/system/privateManager/userLoginAggregate/other/login"
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# 发送 POST 请求
|
# 发送 POST 请求
|
||||||
response = requests.post(url, headers=headers, data=json.dumps(payload))
|
response = requests.post(url, headers=headers, data=json.dumps(payload))
|
||||||
|
|
||||||
# 检查状态码
|
# 打印调试信息(可选)
|
||||||
response.raise_for_status()
|
print("Status Code:", response.status_code)
|
||||||
|
print("Response Text:", response.text)
|
||||||
|
|
||||||
# 转为 JSON
|
# 解析响应
|
||||||
result = response.json()
|
result = response.json()
|
||||||
|
|
||||||
# 从响应中解析 token
|
# 正确获取 token(你原来的写法 result.post(...) 是错误的)
|
||||||
token = result.get("data", {}).get("token")
|
token = result.get("data", {}).get("token")
|
||||||
|
|
||||||
if token:
|
if token:
|
||||||
print("登录成功,Token:", token)
|
print("✅ 登录成功,Token:", token)
|
||||||
else:
|
else:
|
||||||
print("未获取到 Token,响应内容:", result)
|
print("❌ 未获取到 Token,响应内容:", result)
|
||||||
|
|
||||||
except requests.RequestException as e:
|
except requests.RequestException as e:
|
||||||
print("请求异常:", e)
|
print("🌐 请求异常:", e)
|
||||||
except ValueError:
|
except json.JSONDecodeError:
|
||||||
print("响应不是合法的 JSON 格式")
|
print("🧩 响应不是合法的 JSON 格式")
|
||||||
|
except ValueError as ve:
|
||||||
|
print("⚠️ 加密参数错误:", ve)
|
||||||
Loading…
Reference in New Issue