예제 #1
0
def main(file, substitution=None, save=None, output=None):
    try:
        if file is None:
            raise FileNotFoundError('Select file for encrypt.')
    except FileNotFoundError as e:
        print(e)
        return e

    if substitution:
        subst = read_json_file(substitution, 'utf-8')
    else:
        subst = generate_substitution('A-Za-z')
    if not file:
        result = code_stdin(subst)
    else:
        result = code_text_from_file(file, 'utf-8', subst)

    if save:
        write_json_in_file('substitution.txt', subst, 'utf-8')

    if output:
        with open(output, 'w', encoding='utf-8') as file:
            file.write(result)
    else:
        sys.stdout.write(result)
예제 #2
0
 def substitution_dialog(self):
     text, ok = QInputDialog.getText(
         main_window,
         'Substitution',
         'Enter your substitution:',
         text='ABCDEFGHIJKLMNOPQRSTUVWXYZ'.lower())
     if ok:
         subst = OrderedDict(zip(string.ascii_lowercase, list(str(text))))
         write_json_in_file('subst.txt', subst, 'utf-8')
         self.subst = 'subst.txt'
예제 #3
0
    def test_file_to_update_in_wrong_lang(self):
        rus_text_info = l.TextInfo(self.alph_rus, ENCODING, self.big_rus_file)
        rus_count_info = rus_text_info.find_info(100)
        l.write_json_in_file(
            OUTPUT_FILE,
            rus_count_info.make_count_dict(),
            ENCODING)

        en_text_info = l.TextInfo(self.alph_en, ENCODING, self.first_file)
        en_count_info = en_text_info.find_info(100)
        en_count_info.update_count_info(
            OUTPUT_FILE, en_text_info.alph, 100, ENCODING)
        self.assertDictEqual(self.count_first, en_count_info.make_count_dict())
예제 #4
0
    def test__frequency_updated(self):
        text_info = l.TextInfo(self.alph_en, ENCODING, self.first_file)
        count_info = text_info.find_info(100)
        l.write_json_in_file(
            OUTPUT_FILE,
            count_info.make_count_dict(),
            ENCODING)

        new_text_info = l.TextInfo(self.alph_en, ENCODING, self.second_file)
        new_count_info = new_text_info.find_info(100)
        new_count_info.update_count_info(
            OUTPUT_FILE, new_text_info.alph, 100, ENCODING)
        frequency = new_count_info.make_frequency_dict()
        self.assertDictEqual(frequency, self.frequency_updated)
예제 #5
0
    def test_change_top_words(self):
        text_info = l.TextInfo(self.alph_en, ENCODING, self.first_file)
        count_info = text_info.find_info(10)
        l.write_json_in_file(
            OUTPUT_FILE,
            count_info.make_count_dict(),
            ENCODING)

        new_text_info = l.TextInfo(self.alph_en, ENCODING, self.second_file)
        new_count_info = new_text_info.find_info(10)
        new_count_info.update_count_info(
            OUTPUT_FILE, new_text_info.alph, 3, ENCODING)

        self.assertEqual(3, len(new_count_info.words))
예제 #6
0
def handle_many_files(args, files):
    """
    Process a list of files
    :param args:
    :param files: a list of files' names
    """
    handle_one_object(args, files[0])
    for i in range(1, len(files)):
        text_info = TextInfo(args.alph, args.encoding, input_filename=files[i])
        count_info = text_info.find_info(args.top)
        count_info.update_count_info(args.output, text_info.alph, args.top,
                                     args.encoding)
        updated_dict = count_info.make_count_dict()
        write_json_in_file(args.output, updated_dict, args.encoding)
예제 #7
0
    def test_count_updated(self):
        text_info = learner.TextInfo(self.alph_en,
                                     ENCODING,
                                     input_text=self.first_file)
        count_info = text_info.find_info(100)
        learner.write_json_in_file(OUTPUT_FILE, count_info.make_count_dict(),
                                   ENCODING)

        new_text_info = learner.TextInfo(self.alph_en,
                                         ENCODING,
                                         input_text=self.second_file)
        new_count_info = new_text_info.find_info(100)
        new_count_info.update_count_info(OUTPUT_FILE, new_text_info.alph, 100,
                                         ENCODING)
        updated_dict = new_count_info.make_count_dict()
        self.assertDictEqual(updated_dict, self.count_updated)
예제 #8
0
def learn_single_file(top, update_file = None, file_name=None):
    print('Learning single file')
    if file_name:
        text_info = TextInfo(
            'A-Za-z',
            'utf-8',
            input_filename=file_name)
    count_info = text_info.find_info(top)
    if update_file is not None:
        count_info.update_count_info(
            update_file,
            text_info.alph,
            top,
            'utf-8')
        updated_dict = count_info.make_count_dict()
    else:
        updated_dict = count_info.make_count_dict()
    write_json_in_file('result.txt', updated_dict, 'utf-8')
    print('Done')
예제 #9
0
def learn_multiple_files(top, files, update_file=None):
    global workdone
    print('Learning multiple files: {}'.format(len(files)))
    for i in range(0, len(files)):
        text_info = TextInfo('A-Za-z', 'utf-8', input_filename=files[i])
        count_info = text_info.find_info(top)
        if update_file is not None:
            count_info.update_count_info(
                update_file,
                text_info.alph,
                top,
                'utf-8')
            updated_dict = count_info.make_count_dict()
        else:
            updated_dict = count_info.make_count_dict()
        write_json_in_file('result.txt', updated_dict, 'utf-8')
        workdone = (i+1) / (len(files))
        print("\rProgress: [{0:50s}] {1:.1f}%".format('#' * int(workdone * 50), workdone * 100), end="", flush=True)
        if workdone == 1:
            workdone = 0
예제 #10
0
def handle_one_object(args, file_name=None, text=None):
    """
    Process one file at a time, or a given piece of text
    :param args:
    :param file_name:
    :param text:
    """
    if file_name:
        text_info = TextInfo(args.alph,
                             args.encoding,
                             input_filename=file_name)
    elif text:
        text_info = TextInfo(args.alph, args.encoding, input_text=text)
    count_info = text_info.find_info(args.top)
    if args.update_fn:
        count_info.update_count_info(args.update_fn, text_info.alph, args.top,
                                     args.encoding)
        updated_dict = count_info.make_count_dict()
    else:
        updated_dict = count_info.make_count_dict()
    if args.output:
        write_json_in_file(args.output, updated_dict, args.encoding)
    else:
        pprint(updated_dict)
예제 #11
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()
예제 #12
0
def main():
    parser = argparse.ArgumentParser(
        usage='{} [OPTIONS] ALPHABET FILE'.format(os.path.basename(
            sys.argv[0])),
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
        description='Encode text using substitution cipher')

    subst_group = parser.add_mutually_exclusive_group()

    subst_group.add_argument('-s',
                             '--substitution',
                             type=str,
                             dest='substitution',
                             metavar="FILENAME",
                             help='file with the substitution in json format')
    subst_group.add_argument(
        '-r',
        '--reverse',
        type=str,
        dest='reverse',
        metavar="FILENAME",
        help='reverse substitution from file, and use it to encode text')
    subst_group.add_argument(
        '-g',
        '--generate',
        type=str,
        dest='generate',
        metavar="FILENAME",
        default="subst.txt",
        help='generate substitution and choose where to store it')

    parser.add_argument(
        'alph',
        metavar='alphabet',
        help='language, in which text is written (for example: A-Za-z)')
    parser.add_argument('fn',
                        metavar='FILE',
                        nargs='?',
                        default=None,
                        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(
        '-e',
        '--encoding',
        type=str,
        dest='encoding',
        default='utf-8',
        help='choose FILE (and output file, if key -o is chosen) encoding')

    args = parser.parse_args()
    if not args.alph:
        sys.exit("Error: alphabet must be specified")

    if args.substitution:
        subst = read_json_file(args.substitution, args.encoding)
    elif args.reverse:
        subst = reverse_substitution(
            read_json_file(args.reverse, args.encoding))
    elif args.generate:
        subst = generate_substitution(args.alph)
    else:
        subst = generate_substitution(args.alph)

    if not args.fn:
        result = code_stdin(subst)
    else:
        result = code_text_from_file(args.fn, args.encoding, subst)

    # print("Saving substitution to '{}{}'...".format(os.path.sep, SUBST_FILE))
    if args.generate:
        write_json_in_file(args.generate, subst, args.encoding)
    # print("DONE")

    if args.output:
        with open(args.output, 'w', encoding=args.encoding) as file:
            file.write(result)
    else:
        sys.stdout.write(result)
예제 #13
0
def main():
    parser = argparse.ArgumentParser(
        usage='{} [OPTIONS] ALPHABET STAT FILE'.format(
            os.path.basename(sys.argv[0])),
        description='Decode text, encrypted in substitution cipher')

    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='?',
                        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(
        '-e',
        '--encoding',
        type=str,
        dest='encoding',
        default='utf-8',
        help='choose FILE (and output file, if key -o is chosen) encoding')
    parser.add_argument('-s',
                        '--substitution',
                        type=str,
                        dest='substitution',
                        metavar="FILENAME",
                        help='file where to store right substitution')

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

    parser.add_argument('-p',
                        '--possible',
                        nargs='?',
                        const=10,
                        dest='num',
                        type=int,
                        help='if the usual decryption process has left some '
                        'letters empty - try to guess them, '
                        'using another method, and then choose the best'
                        ' one')

    args = parser.parse_args()
    if not args.alph:
        sys.exit("Error: alphabet must be specified")

    if not isinstance(args.fn, str):
        text = stdin_to_text()
        hacker = SubstitutionHacker(args.alph,
                                    args.stat,
                                    args.encoding,
                                    code_text=text,
                                    top=args.top)
        key = hacker.hack()
        if "_" in key.values() and args.num:
            key = choose_best_key(hacker, args)
        decoded_text = hacker.decode_text(text, key)
    else:
        hacker = SubstitutionHacker(args.alph,
                                    args.stat,
                                    args.encoding,
                                    code_fn=args.fn,
                                    top=args.top)
        key = hacker.hack()
        if "_" in key.values() and args.num:
            key = choose_best_key(hacker, args)
        decoded_text = hacker.decode_file(args.fn, args.encoding, key)

    if args.output:
        with open(args.output, 'w', encoding=args.encoding) as file:
            file.write(decoded_text)
    else:
        print(decoded_text)

    if args.substitution:
        write_json_in_file(args.substitution, key, args.encoding)