コード例 #1
0
ファイル: task5.py プロジェクト: Raashi/cryptoanalysis
def main():
    if op == 'freq':
        exec_freqs()
    elif op == 'enc':
        vig.exec_encrypt()
    elif op == 'dec':
        vig.exec_decrypt()
    elif op == 'af':
        enc = read(argv[2])
        freqs = read_freqs(argv[3])
        alph = read(argv[4])
        length = int(argv[5])
        keys = attack_text(enc, freqs, alph, length)
        write('keys.txt', '\n'.join(keys))
    elif op == 'brute':
        vig.exec_brute()
    elif op == 'freqw':
        text = '\n'.join(map(lambda arg: read(arg).lower(), argv[2:]))
        freqs = frequencies_words(text)
        freqs = map(lambda pair: '{} : {:.4f}'.format(*pair), freqs)
        write('words.txt', '\n'.join(freqs))
    elif op == 'aw':
        enc = read(argv[2])
        alph = read(argv[3])
        length = int(argv[4])
        word = argv[5]
        if not all(c in alph for c in word):
            print('ОШИБКА: все символы слова должны быть в алфавите')
            exit(1)
        attack_word(enc, alph, length, word)
    else:
        print_wrong_op()
コード例 #2
0
def main():
    if op == 'enc':
        vig.exec_encrypt()
    elif op == 'dec':
        vig.exec_decrypt()
    elif op == 'h0':
        text = read(argv[2]).lower()
        alph = read(argv[3])
        freqs, h0 = get_h0(text, alph)
        write_freqs('f.txt', freqs)
        write_freqs('h0.txt', h0)
    elif op == 'hd':
        enc = read(argv[2])
        h0 = read_freqs(argv[3])
        alph = ''.join(map(lambda h0i: h0i[0], h0))
        n1 = int(argv[4])
        n2 = int(argv[5])

        hds = [get_hd(enc, alph, d) for d in range(n1, n2 + 1)]
        n_min, dist_min = None, 1
        for idx, hd in enumerate(hds):
            dist = get_distance(list(map(lambda xi: float(xi[1]), h0)),
                                list(map(lambda yi: yi[1], hd)))
            if dist < dist_min:
                n_min, dist_min = n1 + idx, dist
            print(n1 + idx, ':', dist)
        print('Наименьшее расстояние до H0 при d =', n_min)
    else:
        print_wrong_op()
コード例 #3
0
ファイル: task7.py プロジェクト: Raashi/cryptoanalysis
def main():
    if op == 'enc':
        vig.exec_encrypt()
    elif op == 'dec':
        vig.exec_decrypt()
    elif op == 'freq':
        exec_freqs()
    elif op == 'attack':
        enc = read(argv[2])
        alph = read(argv[3])
        freqs = read_freqs(argv[4])
        length = int(argv[5])
        keys = attack(enc, freqs, alph, length)
        write('keys.txt', '\n'.join(keys))
    elif op == 'brute':
        vig.exec_brute()
    else:
        print_wrong_op()
コード例 #4
0
ファイル: task8.py プロジェクト: Raashi/cryptoanalysis
def main():
    if op == 'enc':
        vig.exec_encrypt()
    elif op == 'dec':
        vig.exec_decrypt()
    elif op == 'bi':
        text = read(argv[2]).lower()
        alph = read(argv[3])
        write('bi.txt', '\n'.join(str_freqs_bigrams(text, alph)))
    elif op == 'attack':
        enc = read(argv[2])
        alph = read(argv[3])
        bi = read(argv[4]).split('\n')
        bi = [[float(el) for el in row.split(' ')] for row in bi]
        length = int(argv[5])
        key = attack(enc, alph, bi, length)
        print('Вычисленный ключ:', key)
        write('keys.txt', key)
    else:
        print_wrong_op()
コード例 #5
0
ファイル: task2.py プロジェクト: Raashi/cryptoanalysis
def main():
    if op == 'gen':
        alph, length = read(argv[2]), int(argv[3])
        seq = ''.join(choice(alph) for _ in range(length))
        write(argv[4], seq)
    elif op == 'eindex':
        seq1, seq2 = read(argv[2]), read(argv[3])
        eindex = get_eindex(seq1, seq2, verbose=True)
        print('Индекс вопадения x 100 : {:.2f}'.format(eindex))
    elif op == 'meindex':
        seq1, seq2 = read(argv[2]), read(argv[3])
        alph = read(argv[4])
        meindex = get_meindex(seq1, seq2, alph, verbose=True)
        print('Средний индекс совпадения x 100 : {:.2f}'.format(meindex))
    elif op == 'enc':
        vig.exec_encrypt()
    elif op == 'dec':
        vig.exec_decrypt()
    elif op == 'analyze':
        enc = read(argv[2])
        analyze_shifts(enc)
    else:
        print_wrong_op()