Skip to content

fix: fit new idshit pwd login#20

Open
syhanjin wants to merge 3 commits into
StupidTrees:masterfrom
syhanjin:fit-idshit-pwd-login
Open

fix: fit new idshit pwd login#20
syhanjin wants to merge 3 commits into
StupidTrees:masterfrom
syhanjin:fit-idshit-pwd-login

Conversation

@syhanjin

Copy link
Copy Markdown
  • 根据新认证逻辑重写登录函数
  • kotlin 代码是 Deepseek 根据原有代码加上认证逻辑 python 代码转换的

新统一身份认证代码的 python 登录逻辑

import httpx
import base64
import os
import random
from bs4 import BeautifulSoup
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad

# 随机字符集 (排除了容易混淆的字符)
AES_CHARS = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678"

def random_string(length):
    return ''.join(random.choice(AES_CHARS) for _ in range(length))

def encrypt_password(password, key):
    if not key:
        return password
    
    # 生成随机前缀和IV
    random_prefix = random_string(64)
    iv = random_string(16).encode('utf-8')
    
    # 处理密钥和加密数据
    key_bytes = key.encode('utf-8')
    data = (random_prefix + password).encode('utf-8')
    
    # 配置AES CBC加密器
    cipher = AES.new(key_bytes, AES.MODE_CBC, iv)
    ct_bytes = cipher.encrypt(pad(data, AES.block_size))
    
    # 返回Base64编码结果
    return base64.b64encode(ct_bytes).decode('utf-8')

def decrypt_password(encrypted, key):
    try:
        # 解码Base64
        ct_bytes = base64.b64decode(encrypted)
        iv = ct_bytes[:16]  # 提取IV
        ct = ct_bytes[16:]  # 实际密文
        
        # 处理密钥
        key_bytes = key.encode('utf-8')
        
        # 配置AES CBC解密器
        cipher = AES.new(key_bytes, AES.MODE_CBC, iv)
        pt = unpad(cipher.decrypt(ct), AES.block_size)
        
        # 返回去除64位随机前缀后的结果
        return pt[64:].decode('utf-8')
    except Exception as e:
        print(f"Decryption error: {e}")
        return encrypted

async def login(username, password):
    client = httpx.AsyncClient()
    resp = await client.get("http://jw.hitsz.edu.cn/cas")
    if resp.status_code != 302:
        return None
    resp = await client.get(resp.headers.get("Location"))
    service = resp.url.params.get("service")
    soup = BeautifulSoup(resp.text)
    formEle = soup.select_one("#pwdFromId")
    url = resp.url.join(formEle.attrs.get("action")).copy_add_param("service", service)
    execution = formEle.select_one("input[name=execution]").attrs.get("value")
    form = {
        "username": username,
        "password": encrypt_password(password, formEle.select_one("#pwdEncryptSalt").attrs.get("value")),
        "capture": "",
        "rememberMe": True,
        "_eventId": "submit",
        "cllt": "userNameLogin",
        "dllt": "generalLogin",
        "lt": "",
        "execution": execution
    }
    resp = await client.post(url, data=form)
    if resp.status_code != 302:
        return None
    resp = await client.get(resp.headers.get("Location"))
    return resp.cookies

 - 根据新认证逻辑重写登录函数
 - kotlin 代码是 Deepseek 根据原有代码加上认证逻辑 python 代码转换的
@syhanjin

Copy link
Copy Markdown
Author

另外,打包前能否顺便改一下版本号,避免需要卸载重装

@syhanjin

syhanjin commented Apr 1, 2025

Copy link
Copy Markdown
Author

通过记事本 coding + docker 编译的方式修改了 login 函数,适配了新教务登录系统。本人账号登录测试通过。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant