input = "CJKDPQZZZLSULOEXFPNMXOSVLJSVRHRMFFOABIKBZFJM"
proxima = "PROXIMA"

def vigenere(key, message):
    message = message.upper().replace(" ", "")
    key = key.upper()*(len(message)//len(key)+1)

    plain = ""
    for i in range(len(message)):
        plain = plain + chr(ord('A') + (ord(message[i]) + ord(key[i])) % 26)
    return plain

def vigenere_1(key, message):
    message = message.upper().replace(" ", "")
    key = key.upper()*(len(message)//len(key)+1)

    plain = ""
    for i in range(len(message)):
        plain = plain + chr(ord('A') + (ord(message[i]) - ord(key[i])) % 26)
    return plain

print("*", vigenere("FUA", 'PRO'))
print("*", vigenere("XGA", 'PRO'))

keys = list()
for i in range(26):
    for j in range(26):
        key = chr(i+ord('A')) + chr(j+ord('A'))
        res = vigenere(key, proxima[:len(key)])
        if input.find(res) == -1:
            continue
        for k in range(26):
            key = chr(i+ord('A')) + chr(j+ord('A')) + chr(k+ord('A'))
            res = vigenere(key, proxima[:len(key)])
            if input.find(res) == -1:
                continue
            for l in range(26):
                key = chr(i+ord('A')) + chr(j+ord('A')) + chr(k+ord('A')) + chr(l+ord('A'))
                res = vigenere(key, proxima[:len(key)])
                if input.find(res) == -1:
                    continue
                for m in range(26):
                    key = chr(i+ord('A')) + chr(j+ord('A')) + chr(k+ord('A')) + chr(l+ord('A')) + chr(m+ord('A'))
                    res = vigenere(key, proxima[:len(key)])
                    if input.find(res) == -1:
                        continue
                    for n in range(26):
                        key = chr(i+ord('A')) + chr(j+ord('A')) + chr(k+ord('A')) + chr(l+ord('A')) + chr(m+ord('A')) + chr(n+ord('A'))
                        res = vigenere(key, proxima[:len(key)])
                        if input.find(res) == -1:
                            continue
                        for o in range(26):
                            key = chr(i+ord('A')) + chr(j+ord('A')) + chr(k+ord('A')) + chr(l+ord('A')) + chr(m+ord('A')) + chr(n+ord('A')) + chr(o+ord('A'))
                            res = vigenere(key, proxima[:len(key)])
                            if input.find(res) == -1:
                                continue
                            keys.append(key)

for key in keys:
    res = vigenere_1(key, input[input.find(vigenere(key, proxima)):])
    print(key, res)

print("")
print(vigenere_1("LOKI", input))

