예제 #1
0
def crack_2x2hill_alt(ctext):
    alphabet = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
    combinations = []
    for i in range(26):
        for j in range(26):
            combinations.append([i, j])
    cvectors = []
    for i in range(0, len(ctext), 2):
        cvectors.append(
            [alphabet.index(ctext[i]),
             alphabet.index(ctext[i + 1])])
    decryption_score = []
    for combo in combinations:
        current_decryption = []
        for block in cvectors:
            current_decryption.append(
                chr(((block[0] * combo[0] + block[1] * combo[1]) % 26) + 65))
        decryption_score.append(chisqr("".join(current_decryption)))
    best_1 = combinations[decryption_score.index(min(decryption_score))]
    decryption_score.remove(min(decryption_score))
    best_2 = combinations[decryption_score.index(min(decryption_score))]
    for i in range(2):
        best_1[i] = str(best_1[i])
        best_2[i] = str(best_2[i])
    key1 = " ".join(best_1) + " " + " ".join(best_2)
    key2 = " ".join(best_2) + " " + " ".join(best_1)
    decry1 = decrypt.hill2x2(ctext, key1)
    decry2 = decrypt.hill2x2(ctext, key2)
    s1 = fitness.score(decry1)
    s2 = fitness.score(decry2)
    if s1 > s2:
        print(decry1)
    else:
        print(decry2)
예제 #2
0
def crack_caesar(ctext):

    shifted = []
    stringsqr = []
    for i in range(26):
        shifted.append(decrypt.caesar(ctext, 26 - i))
        stringsqr.append(chisqr(shifted[i]))  # Calculate Chi^2 Statistics

    key = stringsqr.index(
        min(stringsqr))  # Key will be shift with lowest Chi^2 Statistic

    print("\nTYPE: CAESAR")
    print("KEY: " + str(key))
    decrypted = decrypt.caesar(ctext, 26 - key)
    print(decrypted)
    return process_encryption.restore_punctuation(decrypted)
        def crack_caesar(ctext,pbprogress):

            shifted = []
            stringsqr = []
            for i in range(26):
                shifted.append(decrypt.caesar(ctext, 26 - i))
                stringsqr.append(chisqr(shifted[i]))  # Calculate Chi^2 Statistics
                keystested.config(text=str(i+1))
            bestfitness.config(text=str(round(min(stringsqr))))
            key = stringsqr.index(min(stringsqr))  # Key will be shift with lowest Chi^2 Statistic
            decrypted = decrypt.caesar(ctext, 26 - key)
            pbprogress.set(100)
            bestkeys = []
            allkeys = stringsqr[:]
            for i in range(10):
                bestkeys.append(allkeys.index(min(stringsqr)))
                stringsqr.remove(min(stringsqr))
                keys_top[i].config(text=str(bestkeys[i]),font="Courier 11")
            pb.update()
            cipher_output.delete('1.0',tk.END)
            cipher_output.insert(tk.INSERT,restore(decrypted))
            cipher_output.update()
        def crack_3x3hill(ctext,pbprogress):
            padded = len(ctext)
            if len(ctext) % 3 == 1:
                ctext = ctext + "XX"
                padded = 2
            elif len(ctext) % 3 == 2:
                ctext = ctext + "X"
                padded = 1
            alphabet = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
            combinations = []
            for i in range(26):
                for j in range(26):
                    for k in range(26):
                        combinations.append([i, j, k])
            print(combinations)
            cvectors = []
            for i in range(0, len(ctext), 3):
                cvectors.append([alphabet.index(ctext[i]), alphabet.index(ctext[i + 1]), alphabet.index(ctext[i+2])])
            print(cvectors)
            decryption_score = []
            totalloops = len(combinations)
            count = 0
            for combo in combinations:
                current_decryption = []
                for block in cvectors:
                    current_decryption.append(chr(((block[0] * combo[0] + block[1] * combo[1] + block[2] * combo[2]) % 26) + 65))
                cipher_output.delete('1.0', tk.END)
                cipher_output.insert(tk.INSERT, restore("".join(current_decryption)))
                cipher_output.update()
                count += 1
                keystested.config(text=str(count))
                pbprogress.set(round((count/totalloops)*100))
                pb.update()
                decryption_score.append(chisqr("".join(current_decryption)))
                bestfitness.config(text=str(round(decryption_score[-1])))
            decryption_score_copy = decryption_score[:]
            best_1 = combinations[decryption_score_copy.index(min(decryption_score))]
            decryption_score.remove(min(decryption_score))
            best_2 = combinations[decryption_score_copy.index(min(decryption_score))]
            decryption_score.remove(min(decryption_score))
            best_3 = combinations[decryption_score_copy.index(min(decryption_score))]
            print(best_1)
            print(best_2)
            print(best_3)
            for i in range(3):
                best_1[i] = str(best_1[i])
                best_2[i] = str(best_2[i])
                best_3[i] = str(best_3[i])
            key1 = " ".join(best_1) + " " + " ".join(best_2) + " " + " ".join(best_3)
            key2 = " ".join(best_1) + " " + " ".join(best_3) + " " + " ".join(best_2)
            key3 = " ".join(best_2) + " " + " ".join(best_1) + " " + " ".join(best_3)
            key4 = " ".join(best_2) + " " + " ".join(best_3) + " " + " ".join(best_1)
            key5 = " ".join(best_3) + " " + " ".join(best_1) + " " + " ".join(best_2)
            key6 = " ".join(best_3) + " " + " ".join(best_2) + " " + " ".join(best_1)

            decry = []
            keylist = []
            for key in (key1,key2,key3,key4,key5,key6):
                keylist.append(key)
                decry.append(decrypt.hill3x3(ctext,key))
            s = []
            for decryption in decry:
                s.append(fitness.score(decryption))
            s2 = s[:]
            for i in range(6):
                x = s2.index(max(s))
                if i == 0:
                    if padded == 1:
                        complete = restore(decry[x][:-1])
                    elif padded == 2:
                        complete = restore(decry[x][:-2])
                    else:
                        complete = restore(decry[x])
                    cipher_output.delete('1.0', tk.END)
                    cipher_output.insert(tk.INSERT, complete)
                    cipher_output.update()
                keys_top[i].config(text=keylist[x],font="Courier 7")
                if i != 5:
                    s.remove(max(s))
 def crack_2x2hill(ctext,pbprogress):
     padded = len(ctext)
     if len(ctext) % 2 != 0:
         ctext = ctext + "X"
         padded = 1
     alphabet = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
     combinations = []
     for i in range(26):
         for j in range(26):
             combinations.append([i, j])
     cvectors = []
     for i in range(0, len(ctext), 2):
         cvectors.append([alphabet.index(ctext[i+j]) for j in range(2)])
     decryption_score = []
     totalloops = len(combinations)
     count = 0
     for combo in combinations:
         current_decryption = []
         for block in cvectors:
             current_decryption.append(chr(((block[0] * combo[0] + block[1] * combo[1]) % 26) + 65))
         cipher_output.delete('1.0', tk.END)
         cipher_output.insert(tk.INSERT, restore("".join(current_decryption)))
         cipher_output.update()
         count += 1
         keystested.config(text=str(count))
         pbprogress.set(round((count/totalloops)*100))
         pb.update()
         decryption_score.append(chisqr("".join(current_decryption)))
         bestfitness.config(text=str(round(decryption_score[-1])))
     decryption_score_copy = decryption_score[:]
     best_1 = combinations[decryption_score_copy.index(min(decryption_score))]
     decryption_score.remove(min(decryption_score))
     best_2 = combinations[decryption_score_copy.index(min(decryption_score))]
     for i in range(2):
         best_1[i] = str(best_1[i])
         best_2[i] = str(best_2[i])
     key1 = " ".join(best_1) + " " + " ".join(best_2)
     key2 = " ".join(best_2) + " " + " ".join(best_1)
     decry1 = decrypt.hill2x2(ctext, key1)
     decry2 = decrypt.hill2x2(ctext, key2)
     s1 = fitness.score(decry1)
     s2 = fitness.score(decry2)
     print(padded)
     if s1 > s2:
         cipher_output.delete('1.0', tk.END)
         if padded == 1:
             complete = restore(decry1[:-1])
         else:
             complete = restore(decry1)
         cipher_output.insert(tk.INSERT, complete)
         cipher_output.update()
         keys_top[0].config(text=key1)
         keys_top[1].config(text=key2)
     else:
         cipher_output.delete('1.0', tk.END)
         if padded == 1:
             complete = restore(decry2[:-1])
         else:
             complete = restore(decry2)
         cipher_output.insert(tk.INSERT, complete)
         cipher_output.update()
         keys_top[0].config(text=key2)
         keys_top[1].config(text=key1)