Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for ADFGVX Cipher by Tinus_Trotyl
#### ADFGVX ENCODER ####
#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=# B E G I N #=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#
def encode(message, alphabet, key):
message, cleaned, ch = message.lower(), message, "a"
cleaned = ""
for ch in message :
if "a" <= ch <= "z" or "0" <= ch <= "9" : cleaned = cleaned + ch
cipher =[]
for i in range(0, 36, 6) :
cipher = cipher + [alphabet[i:i+6]]
temp, key = list(key)[::-1], []
for i in range(len(temp)) :
if temp[i:].count(temp[i]) == 1 : key = [temp[i]] + key
coded1, adfgvx = [], 'ADFGVX'
for ch in cleaned :
for i in range(6) :
if ch in cipher[i] :
j = cipher[i].index(ch)
coded1 = coded1 + [adfgvx[i]] + [adfgvx[j]]
break
coded2 = [] + len(key) * [[]]
for i in range(len(key)) : coded2[i] = coded1[i::len(key)]
sortkey, coded3 = sorted(list(key)), []
for i in range(len(key)) :
coded3 = coded3 + coded2[key.index(sortkey[i])]
return "".join(coded3)
#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=# E N D #=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#
#### ADFGVX DECODER ####
#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=# B E G I N #=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#
def decode(code, alphabet, key):
temp, key = list(key)[::-1], []
for i in range(len(temp)) :
if temp[i:].count(temp[i]) == 1 : key = [temp[i]] + key
cipher, adfgvx =[], list("ADFGVX")
for i in range(0, 36, 6) :
cipher = cipher + [alphabet[i:i+6]]
sortkey = sorted(list(key))
lenkey = len(key)
code, code1, code2, code3 = list(code), [], [], []
L1, Ln = len(code)//lenkey, len(code)%lenkey
idx = lenkey * [0]
for i in range(lenkey) :
j = sortkey.index(key[i])
if i >= Ln : idx[j] = L1
else : idx[j] = L1+1
for i in idx :
clmn, code = code[:i], code[i:]
if i < L1 + 1 : clmn = clmn + ["*"]
code1 = code1 + [clmn]
for i in range(lenkey) :
j = sortkey.index(key[i])
code2 = code2 + [code1[j]]
for i in range(L1+1) :
for j in code2 :
code3 = code3 + [j[i]]
while "*" in code3 : code3.remove("*")
message = len(code3)//2 * [" "]
for i in range(len(message)) :
message[i] = cipher[adfgvx.index(code3[2*i])] [adfgvx.index(code3[2*i+1])]
return "".join(message)
#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=# E N D #=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#
May 13, 2017