蘑菇钉 & 工学云 自动打卡（之前的代码被封了，此版本移除Github自动任务)
import requests


# PushPlus
def pushMessage(phone, title, content, token):
    url = 'http://www.pushplus.plus/send?token=' + token + '&title=' + title + '&content=' + content + '&template=html'
    resp = requests.post(url)
    if resp.json()["code"] == 200:
        print('推送消息提醒成功！')
    else:
        print('推送消息提醒失败！')

MessagePush.py
XuanRanDev/Auto-GongXueYun-2
import datetime
import json
import os
import random
import sys
import time
from hashlib import md5


import requests
from utils import AES,UTC as pytz

import MessagePush

pwd = os.path.dirname(os.path.abspath(__file__)) + os.sep

# 设置重连次数
requests.adapters.DEFAULT_RETRIES = 5


def get_plan_id(user, token: str, sign: str):
    url = "https://api.moguding.net:9000/practice/plan/v3/getPlanByStu"
    data = {
        "state": ""
    }
    headers2 = {
        'roleKey': 'student',
        "authorization": token,
        "sign": sign,
        "content-type": "application/json; charset=UTF-8",
        "user-agent": getUserAgent(user)
    }
    res = requests.post(url=url, data=json.dumps(data), headers=headers2)
    return res.json()["data"][0]["planId"]


def getUserAgent(user):
    if user["user-agent"] != 'null':
        return user["user-agent"]
    user["user-agent"] = random.choice(
        [
            'Mozilla/5.0 (Linux; U; Android 9; zh-cn; Redmi Note 5 Build/PKQ1.180904.001) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/71.0.3578.141 Mobile Safari/537.36 XiaoMi/MiuiBrowser/11.10.8',
            'Mozilla/5.0 (Linux; Android 9; MI 6 Build/PKQ1.190118.001; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/76.0.3809.89 Mobile Safari/537.36 T7/11.20 SP-engine/2.16.0 baiduboxapp/11.20.2.3 (Baidu; P1 9)',
            'Mozilla/5.0 (Linux; Android 10; EVR-AL00 Build/HUAWEIEVR-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/74.0.3729.186 Mobile Safari/537.36 baiduboxapp/11.0.5.12 (Baidu; P1 10)',
            'Mozilla/5.0 (Linux; Android 9; JKM-AL00b Build/HUAWEIJKM-AL00b; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/66.0.3359.126 MQQBrowser/6.2 TBS/045130 Mobile Safari/537.36 MMWEBID/8951 MicroMessenger/7.0.12.1620(0x27000C36) Process/tools NetType/4G Language/zh_CN ABI/arm64',
            'Mozilla/5.0 (Linux; Android 8.1.0; PBAM00 Build/OPM1.171019.026; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/76.0.3809.89 Mobile Safari/537.36 T7/11.20 SP-engine/2.16.0 baiduboxapp/11.20.0.14 (Baidu; P1 8.1.0) NABar/2.0',
            'Mozilla/5.0 (Linux; Android 10; LIO-AN00 Build/HUAWEILIO-AN00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/67.0.3396.87 XWEB/1170 MMWEBSDK/200201 Mobile Safari/537.36 MMWEBID/3371 MicroMessenger/7.0.12.1620(0x27000C36) Process/toolsmp NetType/4G Language/zh_CN ABI/arm64'])
    return user["user-agent"]


def getSign2(text: str):
    s = text + "3478cbbc33f84bd00d75d7dfa69e0daa"
    return md5(s.encode("utf-8")).hexdigest()


def parseUserInfo():
    allUser = ''
    if os.path.exists(pwd + "user.json"):
        print("找到配置文件,将从配置文件读取信息！")
        with open(pwd + "user.json", encoding="utf-8") as f:
            lines = f.readlines()
            for line in lines:
                allUser = allUser + line + '\n'
    else:
        return json.loads(os.environ.get("USERS", ""))
    return json.loads(allUser)


def save(user, userId: str, token: str, planId: str, country: str, province: str,
         address: str, signType: str = "START", device: str = "Android", latitude: str = None, longitude: str = None):
    text = device + signType + planId + userId + address
    headers2 = {
        'roleKey': 'student',
        "user-agent": getUserAgent(user),
        "sign": getSign2(text=text),
        "authorization": token,
        "content-type": "application/json; charset=UTF-8"
    }
    data = {
        "country": country,
        "address": address,
        "province": province,
        "city": user["city"],
        "area": user["area"],
        "latitude": latitude,
        "description": user["desc"],
        "planId": planId,
        "type": signType,
        "device": device,
        "longitude": longitude
    }
    url = "https://api.moguding.net:9000/attendence/clock/v2/save"
    res = requests.post(url=url, headers=headers2, data=json.dumps(data))
    return res.json()["code"] == 200, res.json()["msg"]


def encrypt(key, text):
    aes = AES(key.encode("utf-8"))
    res = aes.encrypt(text.encode('utf-8'))
    msg = res.hex()
    return msg


def getToken(user):
    url = "https://api.moguding.net:9000/session/user/v3/login"
    t = str(int(time.time() * 1000))
    data = {
        "password": encrypt("23DbtQHR2UMbH6mJ", user["password"]),
        "phone": encrypt("23DbtQHR2UMbH6mJ", user["phone"]),
        "t": encrypt("23DbtQHR2UMbH6mJ", t),
        "loginType": user["type"],
        "uuid": ""
    }
    headers2 = {
        "content-type": "application/json; charset=UTF-8",
        "user-agent": getUserAgent(user)
    }
    res = requests.post(url=url, data=json.dumps(data), headers=headers2)
    return res.json()


def useUserTokenSign(user):
    phone = user["phone"]
    token = user["token"]
    userId = user["userId"]
    planId = user["planId"]
    signStatus = startSign(userId, token, planId, user, startType=0)
    if signStatus:
        print('警告：保持登录失败，Token失效，请及时更新Token')
        print('重试：正在准备使用账户密码重新签到')
        MessagePush.pushMessage(phone, '工学云设备Token失效',
                                '工学云自动打卡设备Token失效，本次将使用账号密码重新登录签到，请及时更新配置文件中的Token' +
                                ',如不再需要保持登录状态,请及时将配置文件中的keepLogin更改为False取消保持登录打卡，如有疑问请联系邮箱：XuanRanDev@qq.com'
                                , user["pushKey"])
        prepareSign(user, keepLogin=False)


def prepareSign(user, keepLogin=True):
    if not user["enable"]:
        return

    if user["keepLogin"] and keepLogin:
        # 启用了保持登录状态，则使用设备Token登录
        print('用户启用了保持登录，准备使用设备Token登录')
        useUserTokenSign(user)
        return

    userInfo = getToken(user)
    phone = user["phone"]

    if userInfo["code"] != 200:
        print('打卡失败，错误原因:' + userInfo["msg"])
        MessagePush.pushMessage(phone, '工学云打卡失败！',
                                '用户：' + phone + ',' + '打卡失败！错误原因：' + userInfo["msg"],
                                user["pushKey"])
        return

    userId = userInfo["data"]["userId"]
    token = userInfo["data"]["token"]

    sign = getSign2(userId + 'student')
    planId = get_plan_id(user, token, sign)
    startSign(userId, token, planId, user, startType=1)


# startType = 0 使用保持登录状态签到
# startType = 1 使用登录签到
def startSign(userId, token, planId, user, startType):
    hourNow = datetime.datetime.now(pytz.timezone('PRC')).hour
    if hourNow < 12:
        signType = 'START'
    else:
        signType = 'END'
    phone = user["phone"]
    print('-------------准备签到--------------')

    latitude = user["latitude"]
    longitude = user["longitude"]
    if user["randomLocation"]:
        latitude = latitude[0:len(latitude) - 1] + str(random.randint(0, 10))
        longitude = longitude[0:len(longitude) - 1] + str(random.randint(0, 10))

    signResp, msg = save(user, userId, token, planId,
                         user["country"], user["province"], user["address"],
                         signType=signType, device=user['type'],
                         latitude=latitude, longitude=longitude)
    if signResp:
        print('签到成功')
    else:
        print('签到失败')
        if not startType:
            print('-------------签到完成--------------')
            return True

    ######################################
    # 处理推送信息
    pushSignType = '上班'
    if signType == 'END':
        pushSignType = '下班'

    pushSignIsOK = '成功！'
    if not signResp:
        pushSignIsOK = '失败！'

    signStatus = '打卡'

    hourNow = datetime.datetime.now(pytz.timezone('PRC')).hour
    if hourNow == 11 or hourNow == 23:
        signStatus = '补签'

    # 推送消息内容构建

    MessagePush.pushMessage(phone, '工学云' + pushSignType + signStatus + pushSignIsOK,
                            '用户：' + phone + '，工学云' + pushSignType + signStatus + pushSignIsOK
                            , user["pushKey"])

    # 消息推送处理完毕
    #####################################

    print('-------------签到完成--------------')


def signCheck(users):
    for user in users:
        if not user["signCheck"] and user["enable"]:
            continue

        print()
        url = "https://api.moguding.net:9000/attendence/clock/v1/listSynchro"
        if user["keepLogin"]:
            print('          此用户保持登录状态开启，准备使用Token查询          ')
            token = user["token"]
        else:
            print('            此用户保持登录状态关闭，准备登录账号          ')
            token = getToken(user)["data"]["token"]
        header = {
            "accept-encoding": "gzip",
            "content-type": "application/json;charset=UTF-8",
            "rolekey": "student",
            "host": "api.moguding.net:9000",
            "authorization": token,
            "user-agent": getUserAgent(user)
        }
        t = str(int(time.time() * 1000))
        data = {
            "t": encrypt("23DbtQHR2UMbH6mJ", t)
        }
        res = requests.post(url=url, headers=header, data=json.dumps(data))

        if res.json()["msg"] != 'success':
            print('            获取用户打卡记录失败          ')
            continue

        lastSignInfo = res.json()["data"][0]
        lastSignDate = lastSignInfo["dateYmd"]
        lastSignType = lastSignInfo["type"]
        hourNow = datetime.datetime.now(pytz.timezone('PRC')).hour
        nowDate = str(datetime.datetime.now(pytz.timezone('PRC')))[0:10]
        if hourNow <= 12 and lastSignType == 'END' and lastSignDate != nowDate:
            print('            今日未打上班卡，准备补签          ')
            prepareSign(user)
        if hourNow >= 23 and lastSignType == 'START' and lastSignDate == nowDate:
            print('            今日未打下班卡，准备补签          ')
            prepareSign(user)
        print('        Tips：如果没提示上班或者下班补签即代表上次打卡正常          ')
        continue


if __name__ == '__main__':
    users = parseUserInfo()
    hourNow = datetime.datetime.now(pytz.timezone('PRC')).hour
    if hourNow == 11 or hourNow == 23:
        print('----------------------------每日签到检查开始-----------------------------')
        print('          每日11点以及23点为打卡检查，此时间段内自动打卡不会运行          ')
        try:
            signCheck(users)
        except Exception as e:
            print('每日签到检查运行错误！可能与服务器建立连接失败,具体错误原因：' + str(e))
        print('----------------------------每日签到检查完成-----------------------------')
        sys.exit()
    for user in users:
        try:
            prepareSign(user)
        except Exception as e:
            print('工学云打卡失败，错误原因：' + str(e))
            MessagePush.pushMessage(user["phone"], '工学云打卡失败',
                                    '工学云打卡失败, 可能是连接工学云服务器超时,但请别担心，' +
                                    '中午11点以及晚上23点，我们会进行打卡检查，' +
                                    '如未打卡则会自动补签（在打卡检查启用的情况下）。\n\n\n' +
                                    '具体错误信息：' + str(e)
                                    , user["pushKey"])

main.py
XuanRanDev/Auto-GongXueYun-2
from datetime import tzinfo,timedelta

class UTC(tzinfo):
    """UTC"""
    def __init__(self,offset = 0):
        self._offset = offset

    def utcoffset(self, dt):
        return timedelta(hours=self._offset)

    def tzname(self, dt):
        return "UTC +%s" % self._offset

    def dst(self, dt):
        return timedelta(hours=self._offset)
    
    def timezone(str):
        if str == 'UTC':
            return UTC()
        elif str == 'PRC':
            return UTC(8)
        else:
            return None
    

SBox = [0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B,
        0xFE, 0xD7, 0xAB, 0x76, 0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0,
        0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0, 0xB7, 0xFD, 0x93, 0x26,
        0x36, 0x3F, 0xF7, 0xCC, 0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8, 0x31, 0x15,
        0x04, 0xC7, 0x23, 0xC3, 0x18, 0x96, 0x05, 0x9A, 0x07, 0x12, 0x80, 0xE2,
        0xEB, 0x27, 0xB2, 0x75, 0x09, 0x83, 0x2C, 0x1A, 0x1B, 0x6E, 0x5A, 0xA0,
        0x52, 0x3B, 0xD6, 0xB3, 0x29, 0xE3, 0x2F, 0x84, 0x53, 0xD1, 0x00, 0xED,
        0x20, 0xFC, 0xB1, 0x5B, 0x6A, 0xCB, 0xBE, 0x39, 0x4A, 0x4C, 0x58, 0xCF,
        0xD0, 0xEF, 0xAA, 0xFB, 0x43, 0x4D, 0x33, 0x85, 0x45, 0xF9, 0x02, 0x7F,
        0x50, 0x3C, 0x9F, 0xA8, 0x51, 0xA3, 0x40, 0x8F, 0x92, 0x9D, 0x38, 0xF5,
        0xBC, 0xB6, 0xDA, 0x21, 0x10, 0xFF, 0xF3, 0xD2, 0xCD, 0x0C, 0x13, 0xEC,
        0x5F, 0x97, 0x44, 0x17, 0xC4, 0xA7, 0x7E, 0x3D, 0x64, 0x5D, 0x19, 0x73,
        0x60, 0x81, 0x4F, 0xDC, 0x22, 0x2A, 0x90, 0x88, 0x46, 0xEE, 0xB8, 0x14,
        0xDE, 0x5E, 0x0B, 0xDB, 0xE0, 0x32, 0x3A, 0x0A, 0x49, 0x06, 0x24, 0x5C,
        0xC2, 0xD3, 0xAC, 0x62, 0x91, 0x95, 0xE4, 0x79, 0xE7, 0xC8, 0x37, 0x6D,
        0x8D, 0xD5, 0x4E, 0xA9, 0x6C, 0x56, 0xF4, 0xEA, 0x65, 0x7A, 0xAE, 0x08,
        0xBA, 0x78, 0x25, 0x2E, 0x1C, 0xA6, 0xB4, 0xC6, 0xE8, 0xDD, 0x74, 0x1F,
        0x4B, 0xBD, 0x8B, 0x8A, 0x70, 0x3E, 0xB5, 0x66, 0x48, 0x03, 0xF6, 0x0E,
        0x61, 0x35, 0x57, 0xB9, 0x86, 0xC1, 0x1D, 0x9E, 0xE1, 0xF8, 0x98, 0x11,
        0x69, 0xD9, 0x8E, 0x94, 0x9B, 0x1E, 0x87, 0xE9, 0xCE, 0x55, 0x28, 0xDF,
        0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68, 0x41, 0x99, 0x2D, 0x0F,
        0xB0, 0x54, 0xBB, 0x16]
SBoxIV = [0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e,
          0x81, 0xf3, 0xd7, 0xfb, 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87,
          0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb, 0x54, 0x7b, 0x94, 0x32,
          0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e,
          0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49,
          0x6d, 0x8b, 0xd1, 0x25, 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16,
          0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92, 0x6c, 0x70, 0x48, 0x50,
          0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84,
          0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05,
          0xb8, 0xb3, 0x45, 0x06, 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02,
          0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b, 0x3a, 0x91, 0x11, 0x41,
          0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73,
          0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8,
          0x1c, 0x75, 0xdf, 0x6e, 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89,
          0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b, 0xfc, 0x56, 0x3e, 0x4b,
          0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4,
          0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59,
          0x27, 0x80, 0xec, 0x5f, 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d,
          0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef, 0xa0, 0xe0, 0x3b, 0x4d,
          0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61,
          0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63,
          0x55, 0x21, 0x0c, 0x7d]


def _gf2_mul(a: int, b: int, poly: int) -> int:
    ans = 0
    digit_1 = poly.bit_length() - 1
    while b:
        if b & 1:
            ans = ans ^ a
        a, b = a << 1, b >> 1
        if a >> digit_1:
            a = a ^ poly
    return ans


class AES:
    def __init__(self, key: bytes):
        self.aes_type = len(key) * 8
        self._key_r = self._generate_key(key)

    def _generate_key(self, key: bytes) -> list:
        """密钥扩展"""
        Rcon = [0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1B, 0x36]  # 轮常数
        Nr, Nk = 10 + (self.aes_type - 128) // 32, self.aes_type // 32  # Nr：轮数，Nk：密钥长度
        w = [0 for _ in range(4 * (Nr + 1))]  # 轮密钥

        for i in range(Nk):
            w[i] = int.from_bytes(key[4 * i:4 * i + 4], 'big')
        for i in range(Nk, 4 * (Nr + 1)):
            temp = w[i - 1]
            if i % Nk == 0:
                temp = self._split_int(temp)  # 拆分成 4x8bit
                temp = [SBox[temp[1]] ^ Rcon[i // Nk - 1], SBox[temp[2]],
                        SBox[temp[3]], SBox[temp[0]]]
                temp = self._joint_int(temp)  # 合并回 32bit
            elif Nk > 6 and i % Nk == 4:
                temp = self._split_int(temp)  # 拆分成 4x8bit
                temp = [SBox[temp[0]], SBox[temp[1]],
                        SBox[temp[2]], SBox[temp[3]]]
                temp = self._joint_int(temp)  # 合并回 32bit
            w[i] = w[i - Nk] ^ temp

        # 将 w 变成 4x4xNr 形式的矩阵
        key_r = [[[[0] for _ in range(4)] for _ in range(4)] for _ in range(Nr + 1)]
        for t in range(Nr + 1):
            for i in range(4):
                temp = self._split_int(w[4 * t + i])
                for j in range(4):
                    key_r[t][j][i] = temp[j]
        return key_r

    @staticmethod
    def _split_int(n: int) -> list:
        """拆分 32bit 成 4x8bit"""
        return [(n >> 24) & 0xFF, (n >> 16) & 0xFF, (n >> 8) & 0xFF, n & 0xFF]

    @staticmethod
    def _joint_int(b: list) -> int:
        """合并 4x8bit 成 32bit"""
        return (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | b[3]

    def encrypt(self, plaintext: bytes) -> bytes:
        """加密"""
        if len(plaintext) % 16 != 0:
            v = 16 - len(plaintext) % 16
            for _ in range(16 - len(plaintext) % 16):
                plaintext += v.to_bytes(1, 'big')
        state = [[plaintext[4 * j + i] for j in range(4)] for i in range(4)]
        Nr = 10 + (self.aes_type - 128) // 32

        state = self._add_key(state, self._key_r[0])
        for t in range(1, Nr):
            state = self._sub_byte(state, iv=False)  # 字节代替
            state = self._shift_row(state, iv=False)  # 行移位
            state = self._mix_col(state, iv=False)  # 列混淆
            state = self._add_key(state, self._key_r[t])  # 轮密钥加
        state = self._sub_byte(state, iv=False)
        state = self._shift_row(state, iv=False)
        state = self._add_key(state, self._key_r[Nr])
        # 将 state 重新排列成一维的形式
        ciphertext = bytearray(16)
        for i in range(4):
            for j in range(4):
                ciphertext[4 * j + i] = state[i][j]
        return bytes(ciphertext)

    def decrypt(self, ciphertext: bytes) -> bytes:
        state = [[ciphertext[4 * j + i] for j in range(4)] for i in range(4)]
        Nr = 10 + (self.aes_type - 128) // 32

        state = self._add_key(state, self._key_r[Nr])
        for t in range(1, Nr):
            state = self._shift_row(state, iv=True)  # 逆行移位
            state = self._sub_byte(state, iv=True)  # 逆字节代替
            state = self._add_key(state, self._key_r[Nr - t])  # 轮密钥加
            state = self._mix_col(state, iv=True)  # 逆列混淆
        state = self._shift_row(state, iv=True)
        state = self._sub_byte(state, iv=True)
        state = self._add_key(state, self._key_r[0])

        plaintext = bytearray(16)
        for i in range(4):
            for j in range(4):
                plaintext[4 * j + i] = state[i][j]
        return bytes(plaintext)

    @staticmethod
    def _add_key(state, k):
        for i in range(4):
            for j in range(4):
                state[i][j] = state[i][j] ^ k[i][j]
        return state

    @staticmethod
    def _shift_row(state, iv):
        if not iv:
            state[1] = [state[1][1], state[1][2], state[1][3], state[1][0]]
            state[2] = [state[2][2], state[2][3], state[2][0], state[2][1]]
            state[3] = [state[3][3], state[3][0], state[3][1], state[3][2]]
        else:
            state[1] = [state[1][3], state[1][0], state[1][1], state[1][2]]
            state[2] = [state[2][2], state[2][3], state[2][0], state[2][1]]
            state[3] = [state[3][1], state[3][2], state[3][3], state[3][0]]
        return state

    @staticmethod
    def _mix_col(state, iv):
        if not iv:
            matrix = [[0x02, 0x03, 0x01, 0x01],
                      [0x01, 0x02, 0x03, 0x01],
                      [0x01, 0x01, 0x02, 0x03],
                      [0x03, 0x01, 0x01, 0x02]]
        else:
            matrix = [[0x0E, 0x0B, 0x0D, 0x09],
                      [0x09, 0x0E, 0x0B, 0x0D],
                      [0x0D, 0x09, 0x0E, 0x0B],
                      [0x0B, 0x0D, 0x09, 0x0E]]
        res = [[0 for _ in range(4)] for _ in range(4)]
        for i in range(4):
            for j in range(4):
                temp = 0
                for k in range(4):
                    temp = temp ^ _gf2_mul(matrix[i][k], state[k][j], poly=0x11b)
                res[i][j] = temp
        return res

    @staticmethod
    def _sub_byte(state, iv):
        for i in range(4):
            for j in range(4):
                state[i][j] = SBox[state[i][j]] if not iv else SBoxIV[state[i][j]]
        return state
utils.py
XuanRanDev/Auto-GongXueYun-2