Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
30-liner of nice-scattered code solution in Clear category for ADFGVX Cipher by Stensen
from itertools import product, zip_longest
from collections import defaultdict
from operator import itemgetter
ciphertext = ['A', 'D', 'F', 'G', 'V', 'X']
def encode(message, secret_alphabet, keyword):
keyword = ''.join(sorted({i for i in keyword}, key=keyword.index))
secret_alpha_dict = {t: ciphertext[i//6]+ciphertext[i%6] for i, t in enumerate(secret_alphabet)}
encrypted = ''.join([secret_alpha_dict[i] for i in message.lower() if i in secret_alphabet])
encrypted_keys = defaultdict(str)
for i, t in enumerate(encrypted):
encrypted_keys[keyword[i % len(keyword)]] += t
sorted_encrypted_keys = dict(sorted(encrypted_keys.items(), key=itemgetter(0)))
return ''.join(sorted_encrypted_keys.values())
def decode(message, secret_alphabet, keyword):
keyword = ''.join(sorted({i for i in keyword}, key=keyword.index))
secret_keys_dict = {ciphertext[i//6]+ciphertext[i%6]: t for i, t in enumerate(secret_alphabet)}
keys_length = defaultdict(int)
for i in range(len(message)):
keys_length[keyword[i % len(keyword)]] += 1
sorted_keys_length = dict(sorted(keys_length.items(), key=itemgetter(0)))
msg_breakpts = [0]
for i in sorted_keys_length.values():
msg_breakpts.append(i + msg_breakpts[-1])
horizontal_keys_encryption = ''.join(map(''.join,
zip_longest(*[{
c: message[i:j] for c, (i, j) in zip(''.join(sorted(keyword)), zip(msg_breakpts, msg_breakpts[1:]))
}[i] for i in keyword ], fillvalue='')))
return ''.join([
secret_keys_dict[i] for i in map(''.join,
zip(horizontal_keys_encryption[::2], horizontal_keys_encryption[1::2]))])
Nov. 22, 2020