#Assignment 2 solutions import conversions import rc4 import javarandom import os import importlib #grading script for Assignment 2 #Test code specific to this assignment. def solve_6a(module): ciphertext=conversions.b64_to_as(open('HW2_6a_ciphertext.txt','r').read()) key=conversions.hex_to_as('7f65a580cb') return module.rc4_iv_decrypt(key,ciphertext) def solve_6b(module): #get the ciphertext ciphertext=conversions.b64_to_as(open('HW2_6b_ciphertext.txt','r').read()) seed=module.find_key6(ciphertext) initstate=rc4.rc4_initialize(seed) keystream=rc4.rc4_genbytes(initstate,len(ciphertext)) return conversions.xor(ciphertext,keystream) def solve_7(module): ct=conversions.b64_to_as(open('HW2_7_ciphertext.txt','r').read()) start_four=conversions.xor(ct[:4],'FROM') key=start_four+conversions.hex_to_as(module.find_key7(ct)) return 'FROM'+javarandom.encdec(key,ct[4:]) #Run the test code for this assignment. def grade_hw2(student_name,module): print module print 'Problem 6a\n\n' try: print 'Attempting to run student code.' print repr(solve_6a(module)) raw_input('\n\nhit return to continue') print '\n\n' except AttributeError: print student_name+' did not supply or misnamed the function solve_6a\n\n' except Exception as e: print e.message print 'Problem 6b\n' try: print 'Attempting to run student code.' print repr(solve_6b(module)) print '\n\n\n' except AttributeError: print student_name+' did not supply or misnamed the function find_key6\n\n\n' except Exception as e: print e.message raw_input('\n\nhit return to continue') print 'Problem 7\n' try: print repr(solve_7(module)) except AttributeError: print student_name+' did not supply or misnamed the function find_key7\n\n' except Exception as e: print e.message raw_input('\n\nhit return to continue') #first step, extract all files from zip archives, and #remove the zipped files def extract_from_zip(): filelist=os.listdir(os.curdir) for s in filelist: if s[-7:]=='HW2.zip': os.system('unzip '+s) os.system('rm '+s) print 'unzipping '+s #Now go through the directory, and run each #of the required functions def grade_all_submissions(): filelist=os.listdir(os.curdir) for s in filelist: if s[-3:]=='HW2': print 'grading code for '+s[:-3] sublist=os.listdir(os.curdir+'/'+s) #Now this is weird. The directory has to #contain a file called '__init__.py f=open(os.curdir+'/'+s+'/__init__.py','w') f.close() for t in sublist: if t[-6:]=='HW2.py': ## cleanse(t[:-6]) module=importlib.import_module(s+'.'+s) #print 'imported '+ s grade_hw2(t[:-6],module) print 'Done testing code for '+t[:-6]+'.\n\n' raw_input('hit return to continue\n\n') def do_it(): extract_from_zip() grade_all_submissions()