In [3]:
b64d={0:"A",16:"Q",32:"g",48:"w",1:"B",17:"R",33:"h",49:"x",2:"C",18:"S",34:"i",50:"y",3:"D",19:"T",35:"j",51:"z",4:"E",20:"U",36:"k",52:"0",5:"F",21:"V",37:"l",53:"1",6:"G",22:"W",38:"m",54:"2",7:"H",23:"X",39:"n",55:"3",8:"I",24:"Y",40:"o",56:"4",9:"J",25:"Z",41:"p",57:"5",10:"K",26:"a",42:"q",58:"6",11:"L",27:"b",43:"r",59:"7",12:"M",28:"c",44:"s",60:"8",13:"N",29:"d",45:"t",61:"9",14:"O",30:"e",46:"u",62:"+",15:"P",31:"f",47:"v",63:"/"}
nb64d= dict (zip(b64d.values(),b64d.keys()))
In [4]:
def b642ascii(s):
out=[]
for i in xrange(len(s)/4):
c=s[4*i:(4*i)+4]
#print c
n=0
nulls=0
for z in c:
if z!="=":
n=n<<6 | nb64d[z]
else:
nulls+=1
n=n<<6 | 0
c1=(n&16711680)>>16
c2=(n&65280)>>8
c3=n&255
cs=[c1,c2,c3]
for i in range(3-nulls):
out.append(chr(cs[i]))
return "".join(out)
b642ascii("YW55IGNhcm5hbCBwbGVhcw==")
Out[4]:
In [5]:
def hamming(s1,s2):
b1=str2bin(s1)
b2=str2bin(s2)
b=b1^b2
return ones(b)
def str2bin(s):
o=0
for c in s:
o=o << 8 | ord(c)
return o
def ones(n):
w = 0
while (n):
w += 1
n &= n - 1
return w
In [6]:
hamming("this is a test","wokka wokka!!!")
Out[6]:
In [7]:
out=""
f=open("6.txt","r")
for line in f:
out+=line.strip()
In [44]:
from numpy import mean
s=b642ascii(out)
ksd={}
for keysize in xrange(1,40):
numbytes=8*keysize
numchars=(1+(keysize/4))*4
c1=s[:keysize]
c2=s[keysize:2*keysize]
c3=s[2*keysize:3*keysize]
c4=s[3*keysize:4*keysize]
c5=s[4*keysize:5*keysize]
diff=mean([hamming(c1,c2)/float(keysize),hamming(c1,c3)/float(keysize),hamming(c2,c3)/float(keysize),hamming(c4,c5)/float(keysize),hamming(c2,c4)/float(keysize),hamming(c1,c5)/float(keysize)])
ksd[keysize]=diff
In [45]:
print ksd
In [10]:
print len(b642ascii(out))
In [ ]:
#key length 5 - maybe 2?
In [46]:
s=b642ascii(out)
#s="teststringnew"
keysize=29
blocks=[]
for i in xrange((len(s)/keysize)+1):
if i!=len(s)/keysize:
blocks.append(s[i*keysize:(i+1)*keysize])
else:
if len(s[i*keysize:])>0:
blocks.append(s[i*keysize:])
print len(blocks)
In [47]:
newblocks=[]
for i in xrange(keysize):
newblocks.append([])
for block in blocks:
for j in xrange(len(block)):
newblocks[j].append(block[j])
print len(newblocks)
In [15]:
lf={
"a":0.08167,
"b":0.01492,
"c":0.02782,
"d":0.04253,
"e":0.12702,
"f":0.02228,
"g":0.02015,
"h":0.06094,
"i":0.06966,
"j":0.00153,
"k":0.00772,
"l":0.04025,
"m":0.02406,
"n":0.06749,
"o":0.07507,
"p":0.01929,
"q":0.00095,
"r":0.05987,
"s":0.06327,
"t":0.09056,
"u":0.02758,
"v":0.00978,
"w":0.02360,
"x":0.00150,
"y":0.01974,
"z":0.00074}
In [16]:
from math import sqrt
def computehistogram(block):
myhist={}
chars=0
for k in lf:
myhist[k]=0
for c in block:
c=c.lower()
if c in myhist:
chars+=1
myhist[c]+=1
for k in myhist:
myhist[k]=myhist[k]/float(chars)
return(myhist)
def comparehist(hist):
rmse=0
for k in hist:
rmse+=(lf[k]-hist[k])**2
return rmse
In [17]:
h= computehistogram(newblocks[0])
print h
total=0
for k in h:
total+=h[k]
print total
print comparehist(h)
In [51]:
key=[]
keyds=[]
for block in newblocks:
minscore=float("infinity")
bestc=None
keyd={}
for keyc in range(32,123):
decrypt=map(lambda x: chr(ord(x)^keyc),block)
score=comparehist(computehistogram(decrypt))
keyd[chr(keyc)]=score
#print score
if score<minscore:
minscore=score
bestc=chr(keyc)
key.append(bestc)
keyds.append(keyd)
print key
In [50]:
key=[]
keyds=[]
for i in xrange(keysize):
key.append([])
blocknum=1
for block in newblocks:
minscore=float("infinity")
bestc=None
keyd={}
for i in range(32,122):
cur=map(chr,map(lambda x: ord(x)^i, block))
if sum(map(lambda x: (x>=65 and x<=90) or (x>=97 and x<=122), map(ord, cur)))/float(len(cur))>0.76000:
#print "Key: " + str(chr(i))
key[blocknum-1].append(chr(i))
blocknum+=1
print key
keylist=key
In [ ]:
key=[]
keyds=[]
for i in xrange(keysize):
key.append([])
blocknum=1
for block in newblocks:
minscore=float("infinity")
bestc=None
keyd={}
for i in range(32,91):
cur=map(chr,map(lambda x: ord(x)^i, block))
if sum(map(lambda x: x>=65 and x<=122, map(ord, cur)))/float(len(cur))>0.6200:
#print blocknum
#print "Key: " + str(chr(i))
key[blocknum-1].append(chr(i))
blocknum+=1
print key
keylist=key
In [244]:
import operator
sorts=[]
for d in keyds:
sorted_x = sorted(d.items(), key=operator.itemgetter(1))
sorts.append(sorted_x[:10])
In [20]:
from hex2b64 import hexstring2ascii, repeatkeyxor
def decryptxor(k,s):
return repeatkeyxor(k,s,tohex=False)
In [245]:
print sorts
In [184]:
#print hexstring2ascii(repeatkeyxor("".join(key),s))
print "".join(key)
#print s
key=['i', 'n', 'n', 'e', 'r']
for i in range(len(sorts[0])):
for j in range(len(sorts[0])):
for k in range(len(sorts[0])):
for l in range(len(sorts[0])):
for m in range(len(sorts[0])):
key=[sorts[0][i][0],sorts[1][j][0],sorts[2][k][0],sorts[3][l][0],sorts[4][m][0]]
temp=decryptxor("".join(key),s)
if " the " in temp:
print key
print temp
In [55]:
#key=["I","I","N","I","O"]
#key=["t","t","r","t","o"]
#for x in ['c', 'd', 'e', 'h', 'i', 'n', 'o', 's', 't', 'x']:
#key[0]=x
print decryptxor("Terminator X: Bring the noise",s)
#print "------"
In [ ]: