def shorten_xor_key(key):
    """
    Return shortened XOR key if the key has a cyclic pattern
    Used by guess_multibyte_xor_keys()
    """
    key_len = [1, 2, 4, 8, 16, 32, 64, 128]

    for i in range(0, len(key_len)):
        cyclic = True
        for j in range(key_len[i], 256, key_len[i]):
            if key[0:key_len[i]] != key[j:j+key_len[i]]:
                cyclic = False
                break

        if cyclic:
            return key[0:key_len[i]]

    return key