Пример #1
0
def code_decode(text, language):
    """
    Code and then decode text in a given language, using reversed substitution
    :param text:
    :param language:
    :return:
    """
    subst = e.generate_substitution(language)
    code = e.code(text, subst)
    rev_subst = e.reverse_substitution(subst)
    return e.code(code, rev_subst)
Пример #2
0
def main(stat, filename, iterations, top, debug):
    global workdone
    if stat is None:
        raise FileNotFoundError('Select file with statistics.')
    if filename is None:
        raise FileNotFoundError('Select file with ecrypted text.')
    if iterations is None:
        raise Exception('Iterations')

    print(
        'Stat file: {}\nEncrypted file: {}\nIterations: {}\nPopular words: {}\n'
        .format(stat, filename, iterations, top))
    with open(filename, 'rb') as file:
        original_text = file.read().decode('utf-8')
    coded_text = encryptor.code(original_text,
                                encryptor.generate_substitution('A-Za-z'))

    result = {}
    for n in range(1, iterations + 1):
        sample = coded_text[:n * 50]
        original_sample = original_text[:n * 50]
        data = Decryptor('A-Za-z',
                         stat,
                         'utf-8',
                         text_input=sample,
                         top=top,
                         benchmark=True)
        data.decrypt()
        decoded_sample = data.decode_text(sample)
        diff = count_diff(decoded_sample, original_sample)
        res = ((len(sample) - diff) * 100) / len(sample)
        result[n * 50] = res
        if debug:
            print(
                "\rTotal text's length: {}, Piece's length: {}, Iteration: {}, Recognized: {}%;\n"
                .format(str(len(coded_text)), str(n * 25), str(n),
                        str(round(res, 2))),
                end="",
                flush=True)
        if len(coded_text) < n * 50:
            break
        workdone = n / (iterations)
        print("\rProgress: [{0:50s}] {1:.1f}%".format('#' * int(workdone * 50),
                                                      workdone * 100),
              end="",
              flush=True)
        if workdone == 1:
            workdone = 0
    print(decoded_sample)
    plt.figure()
    plt.plot(list(result.keys()), list(result.values()))
    plt.xlabel("Text's length")
    plt.ylabel("Success rate, %")
    plt.title("Benchmark")
    plt.grid(True)
    plt.savefig("benchmark.png")
    coords = list(result.keys()), list(result.values())
    return coords
Пример #3
0
 def test_decode_from_file(self):
     subst = e.generate_substitution(self.alph_en)
     code = e.code_text_from_file(make_big_en_file(), 'utf-8', subst)
     rev_subst = e.reverse_substitution(subst)
     with make_big_en_file() as f:
         f.seek(0)
         original = f.read().decode(ENCODING)
     decode = e.code(code, rev_subst)
     self.assertEqual(original, decode)
Пример #4
0
 def test_decode_from_file(self):
     subst = e.generate_substitution(self.alph_en)
     with open('Harry.txt', 'rb') as f:
         self.file = f.read().decode('utf-8')
     code = e.code_text_from_file('Harry.txt', 'utf-8', subst)
     rev_subst = e.reverse_substitution(subst)
     decode = e.code(code, rev_subst)
     print(decode)
     self.assertEqual(self.file, decode)
Пример #5
0
 def decode_text(self, text, key=None):
     """
     Decode given text
     :param text:
     :param key:
     :return:
     """
     if not key:
         key = self.key
     return code(text, key)
Пример #6
0
def main():
    parser = argparse.ArgumentParser(
        usage='{} [OPTIONS] ALPHABET STAT FILE'.format(
            os.path.basename(sys.argv[0])),
        description='count the percentage of correctly guessed letters')

    parser.add_argument(
        'alph',
        metavar='alphabet',
        help='language, in which text is written (for example: A-Za-z)')

    parser.add_argument(
        'stat',
        metavar='stat',
        help='statistics of the language')

    parser.add_argument(
        'fn',
        metavar='FILE',
        nargs='?',
        type=argparse.FileType('r'),
        default=sys.stdin,
        help='name of the file with text')

    parser.add_argument(
        '-o',
        '--output',
        type=str,
        dest='output',
        metavar="FILENAME",
        help='choose, where to store output')

    parser.add_argument(
        '-d',
        '--debug',
        action='store_true',
        dest='debug',
        help='enable debug output')

    parser.add_argument(
        '-n',
        '--num',
        type=int,
        dest='num',
        default=51,
        help='choose a quantity of iterations')

    parser.add_argument(
        '-e',
        '--encoding',
        type=str,
        dest='encoding',
        default='utf-8',
        help='choose FILE (and output file, if key -o is chosen) encoding')

    parser.add_argument('-t', '--top', type=int, dest='top', default=15000,
                        help='choose, how many popular words will be stored')

    args = parser.parse_args()
    with open(args.fn.name) as file:
        original_text = file.read()
    coded_text = e.code(original_text,
                        e.generate_substitution(args.alph))

    result = {}
    for n in range(1, args.num):
        sample = coded_text[:n * 25]
        original_sample = original_text[:n * 25]
        hacker = SubstitutionHacker(
            args.alph,
            args.stat,
            args.encoding,
            code_text=sample,
            top=args.top)
        hacker.hack()
        decoded_sample = hacker.decode_text(sample)
        diff = count_diff(decoded_sample, original_sample)
        res = ((len(sample) - diff) * 100) / len(sample)
        result[n * 25] = res
        if args.debug:
            print("Text\'s length {0}; Sample\'s length; {1} n is {2}; "
                  "Correctly guessed {3}%;"
                  .format(str(len(coded_text)), str(n * 25), str(n),
                          str(round(res, 2))))
        if len(coded_text) < n * 25:
            break

    if args.output:
        write_json_in_file(args.output, result, args.encoding)
    else:
        pprint(result)
    print("Last variant:\n", decoded_sample)

    plt.figure()
    plt.xlabel("Text's length")
    plt.ylabel("Success rate, %")
    plt.scatter(list(result.keys()), list(result.values()))
    plt.grid(True)
    plt.savefig("dependency_chart.png")
    plt.show()
Пример #7
0
 def decode_text(self, text, key=None):
     if not key:
         key = self.key
     return code(text, key)