Esempio n. 1
0
def substitute(text,right_key,left_key,decipher):

    #Get the mappings for polybius squares and their inverse
    #The inverse is for lookups.
    plain_table,in_plain = get_polybius(inverse_map=True)
    right_table,in_right = get_polybius(key=right_key,
                                        inverse_map=True)
    left_table,in_left = get_polybius(key=left_key,
                                        inverse_map=True)
    
    #Get the digrams for the text as a list of tuples.
    text = get_digrams(list(text),allow_doubles=True)
    
    out_text = []
    for i in text:
        if not decipher:
            a_coords = plain_table[i[0]]
            b_coords = plain_table[i[1]]
         
            out_text.append(in_right[(a_coords[0],b_coords[1])])
            out_text.append(in_left[(b_coords[0],a_coords[1])])
        else:
            a_coords = right_table[i[0]]
            b_coords = left_table[i[1]]

            out_text.append(in_plain[(a_coords[0],b_coords[1])])
            out_text.append(in_plain[(b_coords[0],a_coords[1])])

    return ''.join(out_text).upper()
Esempio n. 2
0
def create_cipher(args):
    """
    Cipher things in a specified way.
    """
    if args.text:
        text = args.text
    elif args.file:
        text = open(args.file).read()
 
    digrams = get_digrams(list(text),'x') 

    out_text = []

    table = get_polybius(key=args.pass_phrase)
    #Creates an inverse table for lookups
    in_table = {}
    for i in table:
        in_table[table[i]] = i
    
    for i in digrams:
        out_text += get_new_digram(i,table,in_table,args.decipher)

    return ''.join(out_text)