Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for ADFGVX Cipher by Moff
def prepare_message(message):
return ''.join(c for c in message.lower() if c.isalnum())
def prepare_key(key):
result = ''
for c in key:
if c not in result:
result += c
return result
def to_pairs(msg, alpha):
return ''.join(''.join(map(lambda x: 'ADFGVX'[x], divmod(alpha.index(c), 6)))
for c in msg)
def encode(message, alpha, key):
pairs = to_pairs(prepare_message(message), alpha)
key = prepare_key(key)
d = len(key)
m = [pairs[i:i+d].ljust(d) for i in range(0, len(pairs), d)]
cols = [''.join(col).strip() for col in zip(*m)]
return ''.join(cols[key.index(k)] for k in sorted(key))
def from_pairs(pairs, alpha):
return ''.join(alpha['ADFGVX'.index(pair[0]) * 6 + 'ADFGVX'.index(pair[1])]
for pair in [pairs[i:i+2] for i in range(0, len(pairs), 2)])
def decode(cipher, alpha, key):
key = prepare_key(key)
full, rest = divmod(len(cipher), len(key))
height = full + int(bool(rest))
cols = [None] * len(key)
for k in sorted(key):
i = key.index(k)
d = full + int(i < rest)
cols[i] = cipher[:d].ljust(height)
cipher = cipher[d:]
return from_pairs(''.join(''.join(row).strip() for row in zip(*cols)), alpha)
Aug. 11, 2015