Exemple #1
0
def analyze(data, key_size):
    key = [random.choice(string.ascii_lowercase) for _ in range(key_size)]
    while True:
        individuals = generate_individuals(data, key)
        solution = get_solution_from_individuals(individuals)
        if "".join(key) == "".join(solution.new_key):
            break
        key = solution.new_key
        print('Key=%s\t\tFitness=%d' % ("".join(key), solution.fitness))

    print("")
    print("Key=%s" % "".join(key))
    print(vigenere_lib.decrypt("".join(key), data))
Exemple #2
0
 def compute_fitness(self):
     plain = vigenere_lib.decrypt("".join(self._key), self._cipher)
     for w in words_list:
         self._fitness += plain.count(w)
Exemple #3
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import vigenere_lib
import pso

if __name__ == '__main__':
    if len(sys.argv) != 4 or sys.argv[1] not in ["-enc", "-dec", "-pso"]:
        print("Usage:\t%s -enc|-dec plain_file key" % sys.argv[0])
        print("\t%s -pso cipher_file key_size nb_turn" % sys.argv[0])
        sys.exit(1)

    with open(sys.argv[2]) as f:
        data = f.read()

    if sys.argv[1] == "-enc":
        key = sys.argv[3]
        print(vigenere_lib.crypt(key, data))
    elif sys.argv[1] == "-dec":
        key = sys.argv[3]
        print(vigenere_lib.decrypt(key, data))
    elif sys.argv[1] == "-pso":
        key_size = int(sys.argv[3])
        pso.analyze(data.lower(), key_size)