import os


def generate_key_pair():
    """
    生成一对新的密钥a和b，使得a XOR b等于原始key
    :return: (a, b, key) 元组，每个元素都是32字节的字节数组
    """
    # 原始key值
    key = bytes([
        0x5d, 0xf9, 0xef, 0xc4, 0x5d, 0xcc, 0xc7, 0x8d, 0xc9, 0x86, 0x34, 0x11, 0x6f, 0xb4, 0xcf, 0x75,
        0xbf, 0x24, 0x47, 0x9d, 0xd6, 0x5d, 0x83, 0x4b, 0xa6, 0xc0, 0xde, 0x27, 0x91, 0x92, 0xb1, 0x63
    ])

    # 随机生成a
    a = os.urandom(32)

    # 计算b = key XOR a
    b = bytes([key[i] ^ a[i] for i in range(32)])

    return a, b, key


def format_hex_array(data):
    """
    将字节数组格式化为C++风格的十六进制数组
    :param data: 字节数组
    :return: 格式化后的字符串
    """
    return "{" + ",".join([f"0x{b:02x}" for b in data]) + "}"


def generate_new_key_pair():
    """
    生成新的密钥对并打印出来
    """
    a, b, key = generate_key_pair()

    print("原始key:")
    print(format_hex_array(key))
    print("\n新的密钥对:")
    print("a =", format_hex_array(a))
    print("b =", format_hex_array(b))

    # 验证a XOR b是否等于key
    verify_key = bytes([a[i] ^ b[i] for i in range(32)])
    assert verify_key == key, "验证失败：a XOR b 不等于 key"
    print("\n验证成功：a XOR b 等于 key")


if __name__ == "__main__":
    generate_new_key_pair()
