def VICkeystream(keys): # Agent identifier aInd = [ int(i) for i in keys[0] ] # Random number rInd = [ int(i) for i in keys[1] ] # Keyphrase S = keys[2] # Start the key stream kstr = subLists(aInd,rInd) chainAddition(kstr,5) # Use uniqueRank to turn the keyphrase into two lists of 10 numbers n1 = uniqueRank(S[:10]) n2 = uniqueRank(S[10:]) # Add the first list of numbers to the keystream kstr = addLists(n1,kstr) # Used the second list of numbers to encode the keystream nums = [1,2,3,4,5,6,7,8,9,0] kstr = [n2[nums.index(i)] for i in kstr] #print(nums) #print(n2) #print(kstr) chainAddition(kstr,50) #print(kstr) return kstr
def AMSCOExample(): print("Example of the AMSCO Cipher") ptext = "THEQUICKBROWNFOXJUMPSOVERTHELAZYDOG" key = "BIRTH" rank = uniqueRank(key) al = alternating(ptext) print("\nThe Key Is {}\n".format(key)) print("Each letter of the key is ranked to get") print(*rank) print( "\nNow the text is read into the grid by rows as alternating letters and bigrams. The key is placed above.\n" ) print(" ".join([str(i) for i in rank])) printColumns(al, 5, W=4) print( "\nFinally the grid is read off in accordance with column numbers starting with zero, then one, and so on.\n" ) ctext, dtext = example(AMSCO, ptext, key) print(ctext)
def columnarTransportExample(): print("Example of the columnar transport cipher") key = "BIRTHDAY" ptext = "THEQUICKBROWNFOXJUMPSOVERTHELAZYDOG" rank = uniqueRank(key) print("\nThe Key Is {}".format(key)) print("Each letter of the key is ranked to get") print(*rank) print("\nThe plaintext is\n{}".format(ptext)) print( "\nNow the text is read into the grid by rows. The key is placed above.\n" ) print(" ".join([str(i) for i in rank])) for i in groups(ptext, 8): print(" ".join([e for e in i])) print( "\nFinally the grid is read off in accordance with column numbers starting with zero, then one, and so on.\n" ) ctext, dtext = example(columnarTransport, ptext, key, complete=False) print(ctext)
def columnarTransport(text,key,decode=False,complete=False): k = uniqueRank(key) # Determine how many columns numcol = len(k) numrow,rem = divmod(len(text),numcol) longCols = k[:rem] # If complete is selected nulls are added so that the ciphertext fits # perfectly into the grid. if complete == True: if rem > 0: text = addNulls(text,total_len=numcol*(numrow+1)) else: text = addNulls(text,total_len=numcol*numrow) if decode == False: # Read the text into the rows L = groups(text,len(k)) # Read down each column out = [] for col in argsort(k): for row in L: if len(row) > col: out.append(row[col]) return "".join(out) if decode == True: ctr = 0 L = [] for i in range(numcol): if i in longCols: L.append(text[ctr:ctr+numrow+1]) #print("#",text[ctr:ctr+numrow+1],"#") ctr += (numrow+1) else: L.append(text[ctr:ctr+numrow]) #print("#",text[ctr:ctr+numrow],"#") ctr += numrow out = [] for row in range(numrow+1): for col in k: if len(L[col]) > row: out.append( L[col][row] ) return "".join(out)
def disruptedTranspositionExample(): key = "BIRTHDAYS" ptext = "THEYHAVEDISCOVEREDTHATTHEQUICKBROWNFOXJUMPEDOVERTHELAZYDOGFLEENOW" rank = uniqueRank(key) print("Example of Completed Disrupted Transposition\n") print("The Key is: {}".format(key)) print("\nThe plaintext is\n{}".format(ptext)) #ptext = addNulls(ptext,len(key)**2) print("\nWe extend it with nulls to be\n{}".format(ptext)) print() G = ["" for i in key] # Here we copy some code from the cipher to make a nice graphic text = ptext for num in range(len(rank)): L = rank.index(num) + 1 G[num], text = text[:L], text[L:] print("The first half of the text is read into columns like this\n") print(" ".join([str(i) for i in rank])) for row in G: print(" ".join([i for i in row])) print() for num in range(len(rank)): rm = len(key) - len(G[num]) s, text = text[:rm], text[rm:] G[num] += s.lower() print( "\nThe remaining text then fills in the remaining spaces. Shown here in lowercase for ease of reading.\n" ) print(" ".join([str(i) for i in rank])) for row in G: print(" ".join([i for i in row])) print() print( "Then it is read off by columns starting with the one marked zero, then one, and so on.\n" ) print("The completed cipher:") ctext, dtext = example(disruptedTransposition, ptext, key) print(ctext)
def VICtranskeys(kstr,num): # Turn the first ten outputs into a unique list of integers transKey = uniqueRank(kstr[:10]) # Break the rest of the keystream into ten rows G = groups(kstr[10:],10) transLens = [num+kstr[-2], num+kstr[-1]] #print(transLens) out = [] for col in range(10): for row in G: out.append( row[transKey.index(col)] ) if len(out) == sum(transLens): return out[:transLens[0]], out[transLens[0]:] return out[:transLens[0]], out[transLens[0]:]
def VICRank(S): L = uniqueRank(S) return [ (i+1) % 10 for i in L]
def disruptedTransposition(text, key, decode=False, complete=False): if len(text) > len(key)**2: raise Exception( "{} characters cannot fit in transposition with grid size {}". format(len(text), len(key)**2)) rank = uniqueRank(key) if decode == False: # Create a blank grid G = ["" for i in key] # Insert nulls if using the completed version cipher if complete == True: text = addNulls(text, len(key)**2) else: text = addNulls(text, len(key)**2, sep="", alphabet=" ") # Fill each row of the grid until we reach the position of that that row # index in the key. for num in range(len(rank)): L = rank.index(num) + 1 G[num], text = text[:L], text[L:] # Use the remaining letters to fill in the rest of the grid for num in range(len(rank)): rm = len(key) - len(G[num]) s, text = text[:rm], text[rm:] G[num] += s #.lower() # Read off the grid by rows out = "" for x in argsort(rank): for y in range(len(key)): out += G[y][x] out = out.replace(" ", "") return out if decode == True: # We need to know what the grid looks like so we will fill it in as if # we were decrypting it. This is just used to get the row lengths # Should find a better way to do this G = ["" for i in key] text1 = text[:] for num in range(len(rank)): L = rank.index(num) + 1 G[num], text1 = text1[:L], text1[L:] for num in range(len(rank)): rm = len(key) - len(G[num]) s, text1 = text1[:rm], text1[rm:] G[num] += s rowlens = [len(r) for r in G] # Make a grid of blank spaces Gt = [[" " for i in key] for i in key] # Convert the text to a list for easy manipulation text = list(text) # Read down each column placing a letter only if that position in the # row is valid. for col in argsort(rank): for row in range(len(key)): if col < rowlens[row]: Gt[row][col] = text.pop(0) # Merge the rows of the grid X = [] for i in Gt: X.append("".join(i)) # Reach the left and right sides of each row out1 = "" out2 = "" for num in range(len(rank)): L = rank.index(num) + 1 out1 += X[num][:L] out2 += X[num][L:] out = out1 + out2 return out.replace(" ", "")