Skip to content

Commit

Permalink
Fixed implementation and added docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus Færevaag committed Jun 23, 2013
1 parent 6b2255c commit ef4add1
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 19 deletions.
82 changes: 63 additions & 19 deletions project3/breaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,56 +9,100 @@
# Jonathan Becktor (s123094@student.dtu.dk)
#

import time

TIMEINT_START = 1245646800
TIMEINT_END = 1246251599
CIPHERFILE = "ciphertext_sheet3.txt"
START_DATE = '2009-06-22 00:00:00'
END_DATE = '2009-06-28 23:59:59'

a = 69.069
CIPHERFILE = "ciphertext_sheet3.txt"
PLAINFILE = 'plaintext.txt'
KNOWN_PLAINTEXT = ['Snowden',
'Obama',
'Biden',
'Risen',
'Nelson',
'NSA', 'National Security Agency',
'China',
'Whistleblower',
'Hong Kong']

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


def update(s):
"""Update internal state"""
return (a * s + c) % m


def init_key(s):
"""Init key with internal state"""
key = []
for i in xrange(0, 15):
for i in xrange(0, 16):
s = update(s)
key.append(int(float.hex(s)[-6:-4], 16))
key.append(str(bin(s)[-8:]))

return key


def load_cipher():
"""Load ciphertext from file"""
f = open(CIPHERFILE, 'r')
ciphertext = []
for line in f:
for c in line:
ciphertext.append(c)


f.close()
return ciphertext


def encrypt(c, key, i):
# return ord(c)
return int(ord(c)) ^ key[i % 16]
"""Encrypt character with i in key"""
return ord(c) ^ int(key[i % 16], 2)


def main():
# Init s as s_0
key = init_key(TIMEINT_START)

# print key

ciphertext = load_cipher()
def check_plain(plain):
"""Check if text contains any of known plaintexts"""
for word in KNOWN_PLAINTEXT:
if word in plain:
return True
return False

# print ciphertext

for i in xrange(0, 10):
print encrypt(ciphertext[i], key[i], i)
def main():
# 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

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

ciphertext = load_cipher()

plain = ''
for j in xrange(0, len(ciphertext)-1):
c = chr(encrypt(ciphertext[j], key, j))
plain += c

if check_plain(plain):
print "Found plaintext after %i tries!" % (i - start)
print "Key: ",
for k in key:
print chr(int(k, 2)),
print "\n-----------------------------------------------------\n"

print plain
f = open(PLAINFILE, 'w')
f.write(plain)
f.close()

break


if __name__ == '__main__':
Expand Down
19 changes: 19 additions & 0 deletions project3/plaintext.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
NSA analysts spied on own wives and girlfriends

By David Edwards and Muriel Kane

Published: June 18, 2009

According to the reporter who first broke the NSA wiretapping story, there is no proof the agency has scaled back its interception of the personal phone calls and email messages of American citizens as promised by the Obama administration or even that it is being straight with Congress about its activities.

James Risen and Eric Lichtblau revealed the NSA's over-collection of data in an article for the New York Times on Tuesday, noting that one NSA analyst was even found to have been reading the private email of former President Bill Clinton.

Risen told MSNBC's Keith Olbermann the next day that he knew of no other cases like the Clinton incident but that many NSA analysts had been abusing their powers in other ways. "It sounded like, from the former NSA analyst that we interviewed, that it was rare to access the emails of celebrities or famous people," Risen stated, "but that it was fairly routine, according to him, for people to access the emails of girlfriends or wives or other people that they might know."

Risen further addressed the more serious question of whether the Obama administration is complying with its promise to scale back NSA data collection. He said that Rep. Rush Holt (D-NJ), who is the chairman of an intelligence oversight committee, "told us that he is not convinced that the NSA ... has put an end to it. ... He believes that it's not inadvertent and that ... this may be a flagrant problem that is continuing."

Risen did acknowledge, however, that "other people on the Hill seem to disagree with that."

Risen also pointed out there's "a division within Congress" on whether or not they're getting straight answers from the NSA. "Senator Feinstein said today that she believes the NSA is following the law. ... Congressman Holt disagrees with that. ... It's unclear where the oversight is right now."

Risen concluded by noting that �there was a very interesting exchange today in Congress between Attorney General Holder and Senator Russ Feingold. Feingold tried to get Attorney General Holder to agree that the Bush warrantless wiretapping program had been illegal, and Attorney General Holder refused now to use that word 'illegal.' ... That was very interesting. ... It represented a change from the Obama campaign's position on that.

0 comments on commit ef4add1

Please sign in to comment.