# Trifid Relationships by ANCHISES # to accompany Trifid article in Jan-Feb 2008 Computer Column #.....paste ciphertext and crib here.... T="PP#TNPAUF" #ciphertext must begin at start of a period C="aininitss" #crib must be lowercase. #if crib begins part way through period, it must be # preceded by dots. So if crib 'hello' starts in fifth # letter of period 9, C="....hello" len_cipher=len(T) len_crib=len(C) period=9 # insert period here def make_arrays(T,C,len_cipher,len_crib): #.....remove spaces...... t=-1 for j in range (len_crib): if C[j] != ' ': t+=1 pt[t] = C[j] t=-1 for j in range (len_cipher): if T[j] != ' ': t+=1 ct[t] = T[j] if (T[j]=='#'): ct[t]='[' len_cipher=t+1 return(pt,ct,len_cipher) def make_ribbons(pt,ct,len_cipher): nob=len_cipher/period #.......make rib_p[]........ print"period=",period,"len_cipher=",len_cipher,"nob=",nob #...full periods.... t=-1 for j in range(nob): for m in range(0,3,1): for k in range(period): t=t+1 if(pt[(j*period)+k]=='.'): rib_p[t]=-1; else: x=ord(pt[(j*period)+k])-97+(30*m) rib_p[t]=x #.... part period at end... tail=len_cipher-(nob*period) for m in range(3): for j in range(tail): x=ord(pt[(nob*period)+j])-97+(30*m) t=t+1; rib_p[t]=x len_r=t+1 #.....make rib_c[]................... #...full periods.... t=-1 for j in range(nob): for k in range(period): for m in range(3): t=t+1 x=ord(ct[j*period+k])-65+(30*m) rib_c[t]=x #...part period at end..... tail=len_cipher-(nob*period) for j in range(tail): for m in range (3): x=ord(ct[nob*period+j]) - 65 +(30*m) t=t+1; rib_c[t]=x return(rib_p,rib_c,len_r) #.......START............ pt=['.']*500; ct=[0]*1000 rib_p=[0]*500; rib_c=[0]*500 line=[None]*20 #make array line[20][50] index=['A']*27 for x in range(20): line[x]=[None]*50 len_line=[0]*30 grid=[None]*3 for row in range(3): grid[row]=[None]*27 #grid[3][27] len_r=0 pt,ct,len_cipher=make_arrays(T,C,len_cipher,len_crib) #make plain & cipher arrays, pt[] & ct[] rib_p,rib_c,len_r=make_ribbons(pt,ct,len_cipher) #make plain and cipher ribbons, rib_p[] & rib_c[] print "pt[]: ", for j in range (len_cipher): print pt[j], print print "ct[]: ", for j in range (len_cipher): print ct[j], print print"Plain ribbon from crib:" for j in range(3*len_cipher): print rib_p[j], print print"Cipher ribbon from ciphertext:" for j in range(3*len_cipher): print rib_c[j], print;print print "length of ribbon= ",len_r print #...make lines..... nol=-1; for m in range(30): len_line[m]=0; for pos in range(len_r): if(rib_c[pos]==-1 or rib_p[pos]==-1): continue #skip if no letter in ribbon nol+=1 # start a new line line[nol][0]=rib_p[pos] # enter 1st plain-cipher pair line[nol][1]=rib_c[pos] len_line[nol]=2 rib_p[pos]=-1; rib_c[pos]=-1 present=1 while present>0: # add all other pairs to row where one matches present=0 for p in range (pos+1,len_r): #..search rest of ribbon for match if(rib_c[p]==-1 or rib_p[p]==-1): continue #skip if ribbon empty flag=0 for k in range(len_line[nol]): if(rib_p[p]==line[nol][k]): flag=1 # match found to crib part break for k in range(len_line[nol]): if(rib_c[p]==line[nol][k]): flag+=10 # match found to cipher part break if(flag==1): # where match found, add part to line line[nol][len_line[nol]]=rib_c[p] len_line[nol]+=1 present=1 if(flag==10): line[nol][len_line[nol]]=rib_p[p] len_line[nol]+=1 present=1 if(flag>0): rib_p[p]=-1; rib_c[p]=-1 # delete parts from ribbons nol+=1 print "number of lines = ", nol for p in range(nol): print chr(97+p)," ", for q in range(len_line[p]): print line[p][q], print for row in range(3): #initialise grid[][] for col in range(27): grid[row][col]=-1 for row in range(nol): #put discovered parts into grid for col in range(len_line[row]): x=line[row][col] while(x>29): x=x%30 y=line[row][col]/30 grid[y][x]=row #find out how many letters have all 3 parts t=-1 for col in range(27): flag=0 for m in range(3): if(grid[m][col]>-1): flag+=1 if(flag==3): t+=1 index[t]=col len_index=t+1 print print"Here are relationships from the placed crib:" for m in range(len_index): print chr(index[m]+65), print for row in range(3): for col in range(len_index): print chr(97+grid[row][index[col]]), print print