# -*- coding: MacRoman -*- pmq = 4097 # p-q em2 = 11 # e-2 little = 16 lot = 256 cell_length = 3 def make_des(x): # reverse-engineered from un_make_des return pow(x, 3491, 4097) # 3491*11 = 1 mod 3840 (4097 = 17*241, 3840 = 16*240) def un_make_des(x): return pow(x, em2, pmq) def do_it(x): # reverse-engineered from un_do_it return (7-(x-5)*3) % 8 def un_do_it(x): return (3*(7-x)+5) % 8 def do_too_it(x): # reverse-engineered from un_do_too_it return (13-5*x) % 8 def un_do_too_it(x): return (5*(13-x)) % 8 def gen_permute(x, f): new = 0 for temp in xrange(8): if x%2 == 1: new += 2 ** f(temp) x //= 2 return new def permute_it(x): return gen_permute(x, do_it) def waymute_it(x): return gen_permute(x, do_too_it) def un_permute_it(x): return gen_permute(x, un_do_it) ''' new = 0 for temp in xrange(8): if x%2 == 1: new += 2 ** un_do_it(temp) x //= 2 return new ''' def un_waymute_it(x): return gen_permute(x, un_do_too_it) ''' new = 0 for temp in xrange(8): if x%2 == 1: new += 2 ** un_do_too_it(temp) x //= 2 return new ''' def un_happenin(x1, y1, z1): this_num = un_make_des(x1 + (y1 % little) * lot) that_num = un_make_des((z1 * little) + (y1 // little)) return ( this_num % lot, (this_num // lot) + (that_num % little) * little, that_num // little ) def happenin(x1, y1, z1): # reverse-engineered from un_happenin this_num = make_des(x1 + (y1 % little) * lot) that_num = make_des((z1 * little) + (y1 // little)) return ( this_num % lot, (this_num // lot) + (that_num % little) * little, that_num // little ) def roll_the_text(the_text, wend=None): # reverse-engineered from roll_the_text ''' Encrypt a piece of text with the "Agrippa cipher". Its length should be a multiple of 3. ''' # default argument if wend is None: wend = len(the_text) num_cells = wend / cell_length new_text = [] for rip_off in range(num_cells): c1 = chr(permute_it(ord(the_text[3*rip_off]))) c2 = chr(permute_it(ord(the_text[3*rip_off+1]))) c3 = chr(permute_it(ord(the_text[3*rip_off+2]))) temp1, temp2, temp3 = happenin(ord(c1), ord(c2), ord(c3)) new_text.append(chr(temp1)) new_text.append(chr(waymute_it(temp2))) new_text.append(chr(temp3)) return ''.join(new_text) def un_roll_the_text(the_text, wend=None): ''' Decrypt a piece of text using the "Agrippa cipher". Its length should be a multiple of 3. ''' # default argument if wend is None: wend = len(the_text) num_cells = wend / cell_length new_text = [] for rip_off in range(num_cells): c1 = the_text[3*rip_off] c2 = chr(un_waymute_it(ord(the_text[3*rip_off+1]))) c3 = the_text[3*rip_off+2] temp1, temp2, temp3 = un_happenin(ord(c1), ord(c2), ord(c3)) new_text.append(chr(un_permute_it(temp1))) new_text.append(chr(un_permute_it(temp2))) new_text.append(chr(un_permute_it(temp3))) return ''.join(new_text) #print roll_the_text(' ').decode('mac-roman') #print '-----' #print roll_the_text('\rtonight red lanterns are battered,\r\rlaughing,\rin the mechanism. ').decode('mac-roman') #print '-----' poem = un_roll_the_text(open('zi.dat', 'rb').read()) print poem.replace('\r', '\n').decode('mac-roman') print '========================ENCRYPTED TEXT FOLLOWS======================' enclines = [''.join(chr(un_waymute_it(ord(c))) for c in line) for line in poem.split('\r')] delchars = '\x00\x02\x03\x04\x05' # invisible boxchars = '\x01\x06\x07\x08\x0a\x0b\x0c\x0d\x0e\x0f' + ''.join(chr(i) for i in range(16, 32)) # these appear as boxes box = u'\u25AF' for page_start in range(19, len(enclines) - len(enclines) % 17, 17): print for line in enclines[page_start:page_start+17]: dispenc = line dispenc = dispenc.decode('mac-roman') for c in delchars: dispenc = dispenc.replace(c, '') for c in boxchars: dispenc = dispenc.replace(c, box) print dispenc print '------------------------[PAGE BREAK]----------------------------' print '====================PARTIAL DECRYPTION========================' ciphertext = ''';9;9;;9); ; ;9 ; #'1 ;)9; 99 )5 ();;#'#'''' lines = ciphertext.split('\n') for line in lines: print ''.join(chr(waymute_it(ord(c))) for c in line).decode('mac-roman')