Skip to content

Commit

Permalink
Finished doc and cleaned code
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus Færevaag committed Jun 24, 2013
1 parent 189be13 commit 6644f77
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 22 deletions.
26 changes: 21 additions & 5 deletions project3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,25 @@ This will produce a file called plaintext.txt, if it is successful.

## Logic

The script essentially runs a smart brute-force attack.
The script essentially runs a **smart brute-force** attack:

1. We first calculate the relevant time interval the cipher was
created.
2. Load ciphertext from file
3. Start brute-force search for every possible time
1. Initialize key with current time / internal state
2. Encrypt every character in ciphertext with current state
3. Check if result contains any of the words in the array of
possible known plaintext elements.
4. If found, print key and write to file


## Further Help

For further help or explanation please contact one of us by mail and
we'll be happy to help:

* Markus Faerevaag [s123692@student.dtu.dk](mailto:s123692@student.dtu.dk)
* Christian Mathias Rohde Kiaer [s123812@student.dtu.dk](mailto:s123812@student.dtu.dk)
* Jonathan Becktor [s123094@student.dtu.dk](mailto:s123094@student.dtu.dk)

As we know the time period the file was decrypted, we take all the
possible times and iterates over them. For each relevant time encrypt
each letter with the given algorithm, and see if the result contains
any of the words in our array of known plaintext words.
42 changes: 25 additions & 17 deletions project3/breaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@
from sys import argv
import os, time

START_DATE = '2009-06-22 00:00:00'
END_DATE = '2009-06-28 23:59:59'
# Given dates
DATE_FORMAT = '%Y-%m-%d %H:%M:%S'
START_DATE = '2009-06-22 00:00:00'
END_DATE = '2009-06-28 23:59:59'

CIPHERFILE = "ciphertext_sheet3.txt"
PLAINFILE = 'plaintext.txt'
# Given constants
a = 69069
c = 5
m = 2**32

PLAINFILE = 'plaintext.txt'
KNOWN_PLAINTEXT = ['Snowden',
'Obama',
'Biden',
Expand All @@ -27,10 +33,10 @@
'Whistleblower',
'Hong Kong']

# Given constants
a = 69069
c = 5
m = 2**32

def calc_time(date, date_format):
"""Calculates seconds from UNIX epoch (1.1.1970) to given date"""
return int(time.mktime(time.strptime(date, date_format))) - time.timezone


def update(s):
Expand All @@ -48,9 +54,9 @@ def init_key(s):
return key


def load_cipher():
def load_cipher(inputfile):
"""Load ciphertext from file"""
f = open(CIPHERFILE, 'r')
f = open(inputfile, 'r')
ciphertext = []
for line in f:
for c in line:
Expand Down Expand Up @@ -85,22 +91,24 @@ def main():

script, inputfile = argv

# Calculcate time interval
date_format = '%Y-%m-%d %H:%M:%S'
start = int(time.mktime(time.strptime(START_DATE, date_format))) - time.timezone
end = int(time.mktime(time.strptime(END_DATE, date_format))) - time.timezone

# Calculate times
start = calc_time(START_DATE, DATE_FORMAT)
end = calc_time(END_DATE, DATE_FORMAT)

# Load ciphertext from inputfile
ciphertext = load_cipher(inputfile)

# Start bruteforce search
for i in xrange(start, end):
key = init_key(i)

ciphertext = load_cipher()

# Encrypt each character add append to string
plain = ''
for j in xrange(0, len(ciphertext)-1):
c = chr(encrypt(ciphertext[j], key, j))
plain += c

## See if string contains any of known plaintexts
if check_plain(plain):
print "Found plaintext after %i tries!" % (i - start)
print "Key: ",
Expand Down

0 comments on commit 6644f77

Please sign in to comment.