Created
June 29, 2013 14:27
-
-
Save uzername/5891308 to your computer and use it in GitHub Desktop.
Attempt of solution for week1 Crypto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys | |
import pprint | |
from copy import deepcopy | |
def insert_delims(string, every=64): | |
return ' '.join(string[i:i+every] for i in xrange(0, len(string), every)) | |
def XorTwoHexStrings(inp_str1, inp_str2): | |
if (len(inp_str1)!=len(inp_str2)): | |
print len(inp_str1),'!=',len(inp_str2) | |
return 0; | |
final_string=""; | |
for i in xrange(0, len(inp_str1)): | |
TmpRez = hex(int (inp_str1[i],16) ^ int (inp_str2[i],16)).replace('0x',''); | |
final_string+=TmpRez; | |
#print inp_str1[i], '^', inp_str2[i], '=', TmpRez; | |
return final_string; | |
#inp_str1 - string; inp_str2 - strng; returns string | |
def ConvertStrtoHex(inp_str1): | |
#inp_str1 - string; returns string | |
rez_str=""; | |
for i in inp_str1: | |
rez_str+=hex(ord(i)).replace('0x','') | |
return rez_str | |
#obtaining input data from text file. returns python-specific list with data: first sublist contains ciphered texts and second sublist contains text to decipher | |
def read_data(): | |
f=open('t2_input.txt', 'r'); | |
rez=[]; | |
rez.append([]); | |
rez.append([]); | |
sum=0; | |
for line in f: | |
sum+=1; | |
if (sum % 2 == 0): | |
rez[0].append(line[:-1]); | |
print len(rez[0][-1]); | |
tmpstr = rez[0].pop(); | |
rez[1].append(tmpstr); | |
return rez; | |
def space_xor_symbols(file2write): | |
#file2write should be opened | |
space_sym=' '; | |
file2write.write('<h3> XOR space with other symbols </h3>'); | |
#xor space with a-z | |
sym='a'; | |
while (sym!='z'): | |
#b=XorTwoHexStrings(space_sym, sym); | |
b=XorTwoHexStrings(ConvertStrtoHex(space_sym), ConvertStrtoHex(sym)); | |
file2write.write('\''+space_sym+'\''+' xor '+'\''+sym+'\' = '+b+'<br>'); | |
sym=chr(ord(sym)+1); | |
def print_detailed_table(file2write, inplist): | |
file2write.write("<table border=\"1\">"); | |
file2write.write("<tr> <td> </td> <td> </td>"); | |
cntr=0; | |
while (cntr<len(inplist[0][2])/2): | |
file2write.write("<td>"+str(cntr+1)+"</td>"+"<td>"+str(cntr+2)+"</td>"); | |
cntr+=2; | |
file2write.write("</tr>"); | |
for harr in inplist: | |
file2write.write("<tr>"); | |
file2write.write("<tr> <td>"+str(harr[0]+1)+"</td> <td>"+str(harr[1]+1)+"</td>"); | |
cntr=0; | |
while (cntr<len(harr[2])): | |
file2write.write("<td>"+harr[2][cntr]+harr[2][cntr+1]); | |
if (((harr[2][cntr]+harr[2][cntr+1]).decode('hex')>='A') and ((harr[2][cntr]+harr[2][cntr+1]).decode('hex')<='Z')): | |
file2write.write("<br> <span style=\'background-color:salmon\'> \'"+(harr[2][cntr]+harr[2][cntr+1]).decode('hex')+"\' </span>") | |
elif (((harr[2][cntr]+harr[2][cntr+1]).decode('hex')>='a') and ((harr[2][cntr]+harr[2][cntr+1]).decode('hex')<='z')): | |
file2write.write("<br> <span style=\"background-color:salmon\"\'> \'"+(harr[2][cntr]+harr[2][cntr+1]).decode('hex')+"\' </span>") | |
else: | |
file2write.write("<br> ") | |
file2write.write("</td>"); | |
cntr+=2; | |
file2write.write("</tr>"); | |
file2write.write("</table>") | |
def encrypt_heuristics(InpList, LogDaFile, num_cryptorows): | |
#takes full InpList and returns List with partially encrypted plaintext messages | |
#for each byte in cryptomessage... fixing second number in pair and checking the symbols in cyphertexts | |
plaintxtmsgs=[]; | |
for i in InpList: | |
plaintxtmsgs.append([]); | |
#for each byte in cryptomessage... | |
chrplace=0; cntr=0; | |
while (chrplace < len(InpList[0][2])): | |
firstnum=-1; | |
#secondnum=0; | |
numberfound=0; | |
symogrp_found=[]; | |
LogDaFile.write("<p> ==== Character: "+str(cntr)+" ==== </p>"); | |
#for rcrd1 in InpList: | |
# numberfound=0; | |
# for rcrd2 in InpList: | |
# if (rcrd2[1]==rcrd1[1]): | |
# LogDaFile.write("<p> now processing: "+str(rcrd2[0]+1)+";"+str(rcrd2[1]+1)+" </p>") | |
# if (numberfound==-1): | |
# break; | |
#i am not sure to start iterating from 1 or from 0... | |
found=False | |
for c1 in range(0, num_cryptorows): | |
noalpha=0; | |
symbolsofgroup=[]; #crutch essentially | |
#firstnum=-1 | |
LogDaFile.write("<div style=\'color:blue\'> >>> Processing group for "+str(c1+1)+"</div>"); | |
for rcrd2 in InpList: | |
if ((rcrd2[0]==c1) or (rcrd2[1]==c1)): | |
LogDaFile.write("<p> now processing: "+str(rcrd2[0]+1)+";"+str(rcrd2[1]+1)+" ("+(rcrd2[2][chrplace]+rcrd2[2][chrplace+1]).decode('hex')+")"); | |
symbolsofgroup.append((rcrd2[2][chrplace]+rcrd2[2][chrplace+1]).decode('hex')); | |
if (((rcrd2[2][chrplace]+rcrd2[2][chrplace+1]).decode('hex').isalpha() )==False): | |
noalpha+=1; | |
LogDaFile.write(" //NOT A NUMERIC! </p>"); | |
if (noalpha<=1): | |
#found! | |
#print "found!" | |
firstnum=c1; | |
found=True; | |
symogrp_found=symbolsofgroup; | |
break; | |
LogDaFile.write("<div style=\'color:red\'> Message "+str(c1+1)+" has space at "+str(cntr)+"</div>"); | |
if (found==False): | |
LogDaFile.write("<div style=\'color:red\'> Nothing found... </div>") | |
for i in range(len(plaintxtmsgs)): | |
plaintxtmsgs[i].append('?'); | |
else: #decrypt what do we have | |
print "go decrypting! ( Row with space:"+str(firstnum)+")"; | |
#we already know that message c1 (firstnum) has 'space' in cntr position. and other symbols might be obtained by xoring space to obtained alphanumerics | |
plaintxtmsgs[firstnum].append(' '); | |
for rcrd2 in InpList: | |
if ((rcrd2[0]==firstnum) and (rcrd2[1]!=firstnum)): | |
plaintxtmsgs[rcrd2[1]].append(chr(ord(' ') ^ ord((rcrd2[2][chrplace]+rcrd2[2][chrplace+1]).decode('hex')))); | |
elif ((rcrd2[1]==firstnum) and (rcrd2[0]!=firstnum)): | |
plaintxtmsgs[rcrd2[0]].append(chr(ord(' ') ^ ord((rcrd2[2][chrplace]+rcrd2[2][chrplace+1]).decode('hex')))); | |
chrplace+=2; | |
cntr+=1; | |
for line in plaintxtmsgs: | |
LogDaFile.write("<br>") | |
for chr2 in line: | |
LogDaFile.write(chr2); | |
return plaintxtmsgs; | |
def process_data(InpDataList): | |
#Saving List (we don't need InpDataList to be modified) | |
Data2Process = deepcopy(InpDataList); | |
#Find the maximal message len | |
maxlen=0; | |
for line in Data2Process[0]: | |
if len(line)>maxlen: | |
maxlen = len(line); | |
#We are going to 'align' all messages (complete 'em to the longest string with zeros) | |
indx=0; | |
for line in Data2Process[0]: | |
for i in range(0, abs(len(line)-maxlen)): | |
Data2Process[0][indx]+='0'; | |
#print Data2Process[0][indx]; | |
indx+=1; | |
#XOR all obtained lines... | |
f2=open('t2_results.html','w'); | |
f2.write('<html> <title> XOR results </title> '); | |
f2.write('<body>'); | |
f2.write('<div id=\"myID\" style=\"width:1600px; font-family:Courier New">'); | |
f2.write('<table border="1" width="1600px">'); | |
f2.write('<tr> <th> Arg1 </th> <th> (num) </th> <th> Arg2 </th> <th> (num) </th> <th> result </th> </tr>'); | |
cntr1=0; | |
cntr2=0; | |
xor_rezlst=[]; | |
while (cntr1<len(Data2Process[0])): | |
cntr2=cntr1+1; | |
while (cntr2<len(Data2Process[0])): | |
#xor is a symmetrical operation, so order of arguments does not matter | |
tmprez = XorTwoHexStrings(Data2Process[0][cntr1], Data2Process[0][cntr2]); | |
xor_rezlst.append([cntr1, cntr2, tmprez]); | |
f2.write('<tr> <td>'+insert_delims(Data2Process[0][cntr1], 8)+'</td><td>'+str(cntr1)+'</td><td>'+insert_delims (Data2Process[0][cntr2], 8)+'</td>'+'<td>'+str(cntr2)+'</td>'+'<td>'+insert_delims(tmprez, 8)+'</td> </tr>'); | |
cntr2+=1; | |
cntr1+=1; | |
#f2.write(); | |
f2.write('</table>'); | |
f2.write('<h3> XOR results (brief) </h3>') | |
for line in xor_rezlst: | |
f2.write('<p>'+line[2]+'</p>'); | |
space_xor_symbols(f2); | |
#printing the table with xored data | |
print_detailed_table(f2, xor_rezlst); | |
final_msgs = encrypt_heuristics(xor_rezlst, f2, len(Data2Process[0])); | |
f2.write('</div>') | |
#f2.write('<script language=\'JavaScript\'> w=screen.width; d=document.getElementById(\'myID\'); d.style.width=w; </script>') | |
f2.write('</body>'); | |
f2.write('</html>'); | |
return final_msgs; | |
def main(): | |
SomeRez = read_data(); | |
pp=pprint.PrettyPrinter(indent=4); | |
#SomeRez[0].insert(0,SomeRez[1][0]); | |
SomeRez[0].append(SomeRez[1][0]); | |
print '=== Obtained Data ==='; | |
pp.pprint(SomeRez); | |
print '====================='; | |
rez=process_data(SomeRez); | |
print len(SomeRez); | |
print('Your results!'); | |
return 0; | |
if __name__ == '__main__': | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment