def check_key(key: str):
    """Check if a key is valid (512 chars and hex) and remove empty spaces.
    Raises ValueError if the key is invalid."""
    key = key.strip()
    if len(key) != 512:
        raise ValueError(f"The key must have exactly 512 characters (not {len(key)}).")
    try:
        int(key, 16)
    except ValueError:
        raise ValueError("The key must be a valid hexadecimal.")
    return key