from typing import Tuple


def generate_keys(key_num: int) -> Tuple[bytes, bytes]:
    """Taken from Usm.crid, adapted for Python 3.
    Takes a integer that can fit in at most 8 bytes and generates 2 keys.
    Returns 2 bytes type in the following order: Video key and audios key.
    Video key has length of 0x40 bytes, and audios key has length of 0x20 bytes.
    """
    cipher_key = key_num.to_bytes(8, "little")

    key = bytearray(0x20)
    key[0x00] = cipher_key[0]
    key[0x01] = cipher_key[1]
    key[0x02] = cipher_key[2]
    key[0x03] = (cipher_key[3] - 0x34) & 0xFF
    key[0x04] = (cipher_key[4] + 0xF9) & 0xFF
    key[0x05] = cipher_key[5] ^ 0x13
    key[0x06] = (cipher_key[6] + 0x61) & 0xFF
    key[0x07] = key[0x00] ^ 0xFF
    key[0x08] = (key[0x01] + key[0x02]) & 0xFF
    key[0x09] = (key[0x01] - key[0x07]) & 0xFF
    key[0x0A] = key[0x02] ^ 0xFF
    key[0x0B] = key[0x01] ^ 0xFF
    key[0x0C] = (key[0x0B] + key[0x09]) & 0xFF
    key[0x0D] = (key[0x08] - key[0x03]) & 0xFF
    key[0x0E] = key[0x0D] ^ 0xFF
    key[0x0F] = (key[0x0A] - key[0x0B]) & 0xFF
    key[0x10] = (key[0x08] - key[0x0F]) & 0xFF
    key[0x11] = key[0x10] ^ key[0x07]
    key[0x12] = key[0x0F] ^ 0xFF
    key[0x13] = key[0x03] ^ 0x10
    key[0x14] = (key[0x04] - 0x32) & 0xFF
    key[0x15] = (key[0x05] + 0xED) & 0xFF
    key[0x16] = key[0x06] ^ 0xF3
    key[0x17] = (key[0x13] - key[0x0F]) & 0xFF
    key[0x18] = (key[0x15] + key[0x07]) & 0xFF
    key[0x19] = (0x21 - key[0x13]) & 0xFF
    key[0x1A] = key[0x14] ^ key[0x17]
    key[0x1B] = (key[0x16] + key[0x16]) & 0xFF
    key[0x1C] = (key[0x17] + 0x44) & 0xFF
    key[0x1D] = (key[0x03] + key[0x04]) & 0xFF
    key[0x1E] = (key[0x05] - key[0x16]) & 0xFF
    key[0x1F] = key[0x1D] ^ key[0x13]

    audio_t = bytes("URUC", "UTF-8")
    video_key = bytearray(0x40)
    audio_key = bytearray(0x20)

    for i in range(0x20):
        video_key[i] = key[i]
        video_key[0x20 + i] = key[i] ^ 0xFF
        audio_key[i] = audio_t[(i >> 1) % 4] if i % 2 != 0 else key[i] ^ 0xFF

    return bytes(video_key), bytes(audio_key)