Brute Force Transposition

Viewed 27

Hello i have an assignment that I can find out. The questions for the assignment is:

  1. Make a loop that tries to decrypt the ciphertext with all possible keys one at a time.
  2. For each loop, each individual word is looked up in the dictionary. If 85% of the words are found in the dictionary, then it is probably the right key in the current run, and then the loop must be broken.
  3. Decrypt the text with the found key and print it.

I have a code that takes all words from a dictionary and count them. I have linked the dsv file. Hope you can help me.

import csv
import pickle
import math

orddict = {}
item = 0

with open('alle_dkord.csv', 'r', encoding='utf-8') as file:
    reader = csv.reader(file, delimiter=';')
    for row in reader:
        orddict[row[0].upper()] = row[1]
print(len(orddict))
pkfile = open('wordlist.pkl', 'ab')
pickle.dump(orddict, pkfile)
pkfile.close()




def main():
    msg = "This is a cypher text"
    kryptmsg = "Ta h ticesyx ptihse r"
    key = 8
    krypteret_tekst = krypter(key, msg)
    print(krypteret_tekst)
    dekrypteret_tekst = dekrypter(key, kryptmsg)
    print(dekrypteret_tekst)

def krypter(key, msg):
    ciffer_string = [""] * key
    for kolonne in range(key):
        curIndex = kolonne
        while curIndex < len(msg):
            ciffer_string[kolonne] += msg[curIndex]
            curIndex += key
    return''.join(ciffer_string)

def dekrypter(key, kryptmsg):
    numKolonner = int(math.ceil(len(kryptmsg)/float(key)))
    numRows = key
    numOfGreyBox = (numKolonner * numRows) - len(kryptmsg)
    plaintekst = [''] * numKolonner
    kolonne = 0
    row = 0
    for symbol in kryptmsg:
        plaintekst[kolonne] += symbol
        kolonne += 1
        if (kolonne == numKolonner) or (kolonne == numKolonner - 1 and row >= numRows - numOfGreyBox):
            kolonne = 0
            row += 1
    return ''.join(plaintekst)

if __name__ =='__main__':
    main()

The csv file

I have tried to make a loop. But it didn't work

0 Answers
Related