Exemplo n.º 1
0
def crack(text):
    text = text.replace(' ', '')

    best_string = None
    best_score = -10**10

    scorer = NGramScorer('english_quadgrams.txt')

    for i in xrange(26):

        shifted_text = _shift(text, i)
        score = scorer.score(shifted_text)

        if score > best_score:
            best_score = score
            best_string = shifted_text

    return (best_string, best_score)
def crack(text, rounds=10000):
    text = text.replace(' ', '')

    scorer = NGramScorer('english_quadgrams.txt')

    best_map = _build_cunning_map(text)
    best_text = _apply_map(best_map, text)
    best_score = scorer.score(best_text)

    for x in xrange(1, rounds):

        current_map = _shuffle_map(best_map.copy())
        current_text = _apply_map(current_map, text)
        current_score = scorer.score(current_text)

        if current_score > best_score:
            best_score = current_score
            best_map = current_map
            best_text = current_text

    return best_text, best_map
def crack(text, key_length):
    text = text.replace(' ', '')
    text_length = len(text)
    sub_texts = [text[i::key_length] for i in xrange(0, key_length)]

    scorer = NGramScorer('english_monograms.txt')

    best_shifts = []
    best_texts = []
    best_scores = 0

    for sub_text in sub_texts:

        best_text = None
        best_score = -10**10
        best_shift = None

        for i in xrange(26):

            current_text = _shift(sub_text, i)
            current_score = scorer.score(current_text)

            if current_score > best_score:
                best_score = current_score
                best_text = current_text
                best_shift = i

        best_shifts.append(best_shift)
        best_texts.append(best_text)
        best_scores += best_score

    real_best_text = ""
    for i in xrange(0, text_length):
        real_best_text += best_texts[i % key_length][i / key_length]

    return real_best_text, best_shifts, best_scores