In [24]:
#hex to base64
hexd={"0":0,"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":7,"8":8,"9":9,"a":10,"b":11,"c":12,"d":13,"e":14,"f":15}
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:"/"}
inputs="49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d"
#inputs="4d616e2069732064697374696e67756973686564"
inputs="4d616e2069732064697374696e677569736865642c206e6f74206f6e6c792062792068697320726561736f6e2c2062757420627920746869732073696e67756c61722070617373696f6e2066726f6d206f7468657220616e696d616c732c2077686963682069732061206c757374206f6620746865206d696e642c20746861742062792061207065727365766572616e6365206f662064656c6967687420696e2074686520636f6e74696e75656420616e6420696e6465666174696761626c652067656e65726174696f6e206f66206b6e6f776c656467652c2065786365656473207468652073686f727420766568656d656e6365206f6620616e79206361726e616c20706c6561737572652e"
out=""
In [36]:
out=""
def encode(js):
z = (js[0] << 8) | js[1]
z = (z<<8) | js[2]
js=[]
oc1=16515072&z
oc1=oc1>>18
oc2=258048&z
oc2=oc2>>12
oc3=4032&z
oc3=oc3>>6
oc4=63&z
return [oc1,oc2,oc3,oc4]
tc=0
js=[]
for i in xrange(len(inputs)/2):
c=inputs[2*i:(2*i)+2]
j=16*hexd[c[0]]+hexd[c[1]]
js.append(j)
tc+=1
if tc==3:
ocs=encode(js)
js=[]
tc=0
#print ocs
for oc in ocs:
out=out+str(b64d[oc])
if tc!=0:
for v in range(3-tc):
js.append(0)
ocs = encode(js)
for oc in ocs:
out=out+str(b64d[oc])
pass
mys=""
for i in range(3-tc):
mys=mys+"="
out=out[:-(3-tc)]+mys
print out
In [19]:
In [1]:
from hex2b64 import decodehex
In [86]:
s1=decodehex("1c0111001f010100061a024b53535009181c")
s2=decodehex("686974207468652062756c6c277320657965")
#746865206b696420646f6e277420706c6179
In [3]:
s1
Out[3]:
In [60]:
def createbinary(sl):
out=0
for i in range(len(sl)):
out=out<<8 | sl[i]
return out
In [61]:
createbinary(s1)^createbinary(s2)
Out[61]:
In [84]:
def encodehex(n):
out=""
trigger=False
limit=128
for i in range(limit):
if n/(16**(limit-1-i))>=1 or trigger==True:
trigger=True
#print i, n
if i!=limit-1:
out+=str(nhexd[n/(16**(limit-1-i))])
else:
out+=str(nhexd[n])
n=n-((n/(16**(limit-1-i)))*(16**(limit-1-i)))
if n<0:
n=0
#print out
return out
In [87]:
encodehex(createbinary(s1)^createbinary(s2))
Out[87]:
In [79]:
Out[79]:
In [80]:
hexd={"0":0,"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":7,"8":8,"9":9,"a":10,"b":11,"c":12,"d":13,"e":14,"f":15}
nhexd = dict (zip(hexd.values(),hexd.keys()))
In [25]:
17/16
Out[25]:
In [92]:
s=decodehex("1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736")
In [91]:
s
Out[91]:
In [89]:
s1=createbinary(s)
In [81]:
hexstring2ascii(encodehex(65))
Out[81]:
In [73]:
def hexstring2ascii(s):
out=""
for i in xrange(len(s)/2):
c=s[2*i:(2*i)+2]
j=16*hexd[c[0]]+hexd[c[1]]
out+=str(chr(j))
return out
In [107]:
for i in range(20,120):
cur=map(chr,map(lambda x: x^i, s))
if all(map(lambda x: x>=32 and x<=126, map(ord, cur))):
if cur.count("a")/float(len(cur))>0.03 and cur.count("e")/float(len(cur))>0.01 and cur.count(" ")/float(len(cur))>0.01:
print "".join(cur)
print "Key: " + chr(i)
In [132]:
f=open("4.txt","r")
for line in f:
s=decodehex(line)
for i in range(20,120):
cur=map(chr,map(lambda x: x^i, s))
if sum(map(lambda x: x>=32 and x<=126, map(ord, cur)))/float(len(cur))>0.96:
if cur.count("t")+cur.count("T")>cur.count("p")+cur.count("P") and cur.count("e")+cur.count("E")>cur.count("z")+cur.count("Z") and cur.count("e")+cur.count("E")>cur.count("L")+cur.count("l"):
if cur.count("a")/float(len(cur))>0.03 and cur.count("e")/float(len(cur))>0.01 and cur.count(" ")/float(len(cur))>0.01:
print "".join(cur)
print "Key: " + str(chr(i)) + ", Line: " + line
In [140]:
def ascii2hex(c):
o=encodehex(c)
if len(o)==1:
o="0"+o
return o
def repeatkeyxor(key,s):
sl=list(s)
out=[]
for i in xrange(len(sl)):
out.append(ord(sl[i])^ord(key[i%len(key)]))
return "".join(map(ascii2hex,out))
In [145]:
s="Burning 'em, if you ain't quick and nimble\nI go crazy when I hear a cymbal"
k="ICE"
repeatkeyxor(k,s)
Out[145]:
In [143]:
In [ ]: