代码不长呀,没必要弄个文件。
用 cmd 打开即可,运行需要至少一个命令行参数。
[Python] 纯文本查看 复制代码 import sys
def crc16(data):
num = len(data)
if num > 0:
num2 = 0xFFFF
for byte in data:
num2 ^= byte
for _ in range(8):
if (num2 & 1) != 0:
num2 = (num2 >> 1) ^ 0xA001
else:
num2 >>= 1
return num2
else:
return 0
def generate_mac_password(mac):
# Convert MAC address to bytes
array = bytearray(mac.encode('ascii'))
# Calculate CRC16
num = crc16(array)
# Generate password based on CRC16 value
text = f"{num * 7:04X}"
return text[-4:]
def generate_password1(mac, index):
# Generate the base password from MAC address
base_password = generate_mac_password(mac)
# Append the index (converted to hex)
text = base_password + f"{index:02X}"
# Convert the combined text to bytes for CRC calculation
array = bytearray(text.encode('ascii'))
# Calculate CRC16 and append to the password
crc = crc16(array)
return text + f"{crc & 0xFF:02X}"
if __name__ == "__main__":
# print(len(sys.argv))
if len(sys.argv) not in [2, 3]:
print("Usage: python script.py <mac> <textBox_pollCode>")
sys.exit(1)
print(f"输入的本机序列码:{sys.argv[1]}")
mac = sys.argv[1]
textBox_pollCode = "00000000"
if len(sys.argv) == 3:
# Both mac address and textBox_pollCode are provided
textBox_pollCode = sys.argv[2]
if len(textBox_pollCode) < 6:
print("Error: textBox_pollCode should have at least 8 characters")
sys.exit(2)
# Extract index from textBox_pollCode
index = int(textBox_pollCode[4:6], 16)
# Generate the password
generated_password = generate_password1(mac, index)
print(f"对应用户注册码: {generated_password}")
|