Nov-11-2023, 09:32 PM
I'm trying to decipher the text from ADFGV cipher where is matrix, alphabet and key, but I don't know how to fix it if the key has the same characters.
def decrypt_ADFGVX(self, cod, key): """ Decrypt with the ADFGVX cipher. Does not depend on spacing of encoded text """ matrices = list('ADFGVX') chars = [c for c in cod if c in matrices] key = list(key.upper()) sortedkey = sorted(key) order = [key.index(ch) for ch in sortedkey] originalorder = [sortedkey.index(ch) for ch in key] base, extra = divmod(len(chars), len(key)) strides = [base + (1 if extra > i else 0) for i in order] starts = list(accumulate(strides[:-1], lambda x, y: x + y)) starts = [0] + starts ends = [starts[i] + strides[i] for i in range(len(key))] cols = [chars[starts[i]:ends[i]] for i in originalorder] pairs = [] for i in range((len(chars) - 1) // len(key) + 1): for j in range(len(key)): if i * len(key) + j < len(chars): pairs.append(cols[j][i]) decode = dict((v, k) for (k, v) in self.create_encode_dict().items()) return ''.join([decode[pairs[i] + pairs[i + 1]] for i in range(0, len(pairs), 2)])
Attached Files