def crack_block(cipher, n_shifts=None): if n_shifts is not None: gen = lambda: BlockChromo( [random.randint(0, 25) for _ in range(n_shifts)], cipher) else: gen = lambda: BlockChromo( [random.randint(0, 25) for _ in range(random.randint(1, 5))], cipher) trainer = genetic.Trainer(100, gen) for i in range(200): w = trainer.iterate() return ''.join([str(26 - s) + ' ' for s in w.shifts ]) + '| ' + Encryption.block_step(cipher, w.shifts)
def fitness(self): decr = Encryption.block_step(self.cipher, self.shifts) return cipher.score(decr)
Encryption.simple_step(tokens[2], int(tokens[1])) + '\n') elif tokens[0] == "DECRYPT": w.write( Encryption.simple_step(tokens[2], 26 - int(tokens[1])) + '\n') # 1b with open('./output/1b.out', 'w') as w: with open('./input/1b.in') as file: for line in file: tokens = line.split('|') tokens[0] = re.sub(r'\s', '', tokens[0]) tokens[2] = re.sub(r'\s', '', tokens[2]) shifts = [int(s) for s in tokens[1].split()] if tokens[0] == "ENCRYPT": w.write(Encryption.block_step(tokens[2], shifts) + '\n') elif tokens[0] == "DECRYPT": w.write( Encryption.block_step( tokens[2], [26 - shift for shift in shifts]) + '\n') #1c with open('./output/1c.out', 'w') as w: with open('./input/1c.in') as file: for line in file: tokens = line.split('|') tokens[0] = re.sub(r'\s', '', tokens[0]) tokens[1] = re.sub(r'\s', '', tokens[1]) tokens[2] = re.sub(r'\s', '', tokens[2]) if tokens[0] == "ENCRYPT": w.write(Encryption.permutation(tokens[2], tokens[1]) + '\n')