Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for ADFGVX Cipher by ssk8
dedupe = lambda k: [c for i, c in enumerate(k) if c not in k[:i]]
def polybius(sa, enc=True):
letters ='ADFGVX'
polybius_index = [c + letters[i % 6] for i, c in enumerate(''.join([6 * x for x in letters]))]
if enc: return dict(zip(sa, polybius_index))
else: return dict(zip(polybius_index, sa))
def alphebecipher(strng, kw):
table, ciphered = [[] for i in range(len(kw))], []
for i, char in enumerate(strng):
table[i%len(kw)].append(char)
table_dict = dict(zip(kw, table))
for nc in [table_dict[c] for c in sorted(kw)]:
ciphered.extend(nc)
return ciphered
def encode(message, secret_alphabet, keyword):
p = polybius(secret_alphabet)
frac = ''.join([p[c] for c in list(message.lower()) if c in p])
return ''.join(alphebecipher(frac, dedupe(keyword)))
def decode(message, secret_alphabet, keyword):
p = polybius(secret_alphabet, False)
frac = ''.join([c[1] for c in sorted(zip(alphebecipher(range(len(message)), dedupe(keyword)), message))])
return ''.join([p[t] for t in [frac[i*2:i*2+2] for i in range(int(len(frac)/2))]])
Jan. 26, 2017