Beispiel #1
0
def handler():
    cText = ''
    IC = 0
    mono_poly = ''
    key_len = 0 
    while True:
            cmd_ = raw_input("{}{}(CipherAnalysis){} >> ".format(BUNDERLINE,BBLUE,BEND)).strip()
            if cmd_ == 'help' or cmd_ == 'h' or cmd_ == '?':
                help_ = helper.help_('analysis','')
                print help_.format(CLIGHTBLUE,CEND)
            elif cmd_.startswith('show') and cmd_.endswith('info'):
                if __platform__.startswith('Linux') or __platform__.startswith('Darwin'):
                    header = ['Type','Result','Description']
                    rows = [['Cipher Text',cText,'Encrypted message'],
                            ['Incidence of Coincidence',IC,'Incidence of Coincidence prediction'],
                            ['Result',mono_poly,'Prediction whether monoalphabetic or polyalphabetic'],
                            ['Key length',key_len,'Key length prediction for TheVigenereSquare cipher only']]
                    print
                    print table(rows,header,colorfmt='red')
                    print
                else:
                    table_maker = PrettyTable()
                    table_maker.title = "Information Box"
                    table_maker.field_names = ["Type", "Result","Description"]
                    table_maker.add_row(['Cipher Text',cText,'Encrypted message'])
                    table_maker.add_row(['Incidence of Coincidence',IC,'Incidence of Coincidence prediction'])
                    table_maker.add_row(['Result',mono_poly,'Prediction whether monoalphabetic or polyalphabetic'])
                    table_maker.add_row(['Key length',key_len,'Key length prediction for TheVigenereSquare cipher only'])
                    print table_maker
            elif cmd_.startswith('SET') or cmd_.startswith('set'):
                try:
                    msg = cmd_.split()[1]
                    cipher = cmd_.split()[2]
                    if msg and msg == 'message' or msg == 'msg':
                        cText = cipher 
                    else:
                        raise IndexError()
                except IndexError:
                    print BERR + " Please specify message [cipher text]" + BEND
                    print "    ex. SET message 'Cipher Text'\n"
            elif cmd_ == 'execute':
                if cText == '':
                    print BERR + "Execution Error.." + BEND
                    print "Please specify message [cipher text]" 
                IC, key_len = Analysis(cText)
                if IC > 0.06:
                    if IC > 0.052:
                        mono_poly = " More likely monoalphabetic"
                        
                    else:
                        mono_poly =  " Probably monoalphabetic" 
                elif IC < 0.045:
                    mono_poly = "{}".format(random.choice([" More likely polyalphabetic"," Probably polyalphabetic"])) 
                print '\n' + BOKMSG + mono_poly + BEND
                print BOKMSG + ' Your Incidence of Coincidence is >>{} {}'.format(BEND,IC)
                print BOKMSG + ' Your key length for TheVigenereSquare cipher is >>{} {}'.format(BEND,key_len)
            elif cmd_ == 'back':
                break
Beispiel #2
0
def Table_maker(msg, key, result, DecOrEn):
    encOrdec = ['encrypt', 'encryption', 'decrypt', 'decryption']
    if DecOrEn == 'encrypt':
        encOrdec.pop(2)
        encOrdec.pop()
    elif DecOrEn == 'decrypt':
        encOrdec.pop(0)
        encOrdec.pop(0)

    help_msg = helper.help_('helper', 'msg').format(encOrdec[0])
    help_key = helper.help_('helper', 'key').format(encOrdec[1])
    help_result = helper.help_('helper', 'result').format(encOrdec[1])
    if __platform__.startswith('Linux') or __platform__.startswith('Darwin'):
        header = ['Types', 'Information', 'Helper']
        rows = [["Message", msg, help_msg], ["Key", key, help_key],
                ["Result", result, help_result]]
        table_maker = table(rows, header, colorfmt='green')
        return table_maker
    elif __platform__.startswith('Windows'):
        table_maker = PrettyTable()
        table_maker.title = "Information Box"
        table_maker.field_names = ["Types", "Information", "Helper"]
        table_maker.add_row(["Message", msg, help_msg])
        table_maker.add_row(["Key", key, help_key])
        table_maker.add_row(["Result", result, help_result])
        table_maker.align = "l"
        table_maker.align["Information"] = 'c'
        table_maker.align["Types"] = 'c'
        table_maker.max_width = 120
        table_maker.hrules = 1
        return table_maker
Beispiel #3
0
def Analysis(cipherText):
    nums_0 = (0 for i in range(27))
    alp = dict(zip(string.ascii_uppercase,nums_0))
    for letter in cipherText:
        for key,value in alp.iteritems():
            if letter == key:
                alp[key] += 1
    header = ['Letter','Repeat Time']
    rows = []
    for i in string.ascii_uppercase:
      rows += [[i,str(alp.get(i)) + '      ']]
    table_maker = table(rows,header,colorfmt='green')
    print table_maker
    for key,value in alp.iteritems():
        alp[key] = value * (value - 1)
    IC = sum(alp.values()) / (len(cipherText) * len(cipherText) - 1)
    key_len = abs(((0.027 * len(cipherText)) / (len(cipherText) - 1) * IC - 0.038 * len(cipherText) + 0.065))
    IC = "%.6f" % IC
    key_len = "%.2f" % key_len
    return IC,key_len
Beispiel #4
0
def handler():
    while True:
        cmd_encrypt = raw_input("{}{}(Decrypt){} >> ".format(
            BUNDERLINE, BBLUE, BEND)).strip()
        if cmd_encrypt == 'help' or cmd_encrypt == 'h' or cmd_encrypt == '?':
            help_ = helper.help_('decrypt', 'out')
            print help_.format(CLIGHTBLUE, CEND)
        elif cmd_encrypt.startswith('show') and cmd_encrypt.endswith(
                'options'):
            if __platform__.startswith('Linux') or __platform__.startswith(
                    'Darwin'):
                header = ['ID', 'Type', 'Description']
                rows = [
                    [
                        '1', 'The Additive Cipher',
                        'EnciphDeWord using addition'
                    ],
                    [
                        '2', 'The Multiplicative Cipher',
                        'EnciphDeWord using multiplication'
                    ],
                    [
                        '3', 'The Affine Cipher',
                        'EnciphDeWord using combined of addition and multiplication'
                    ],
                    [
                        '4', 'The Hill Digraph Cipher',
                        'EnciphDeWord using 2x2 integer matricess'
                    ],
                    [
                        '5', 'The Hill Digraph Cipher',
                        'EnciphDeWord using 3x3 integer matrices'
                    ],
                    [
                        '6', 'The Vigenere Square (Cipher)',
                        'EnciphDeWord using word or string'
                    ]
                ]

                print
                print table(rows, header, colorfmt='red')
                print
            else:
                table_maker = PrettyTable()
                table_maker.title = "Information Box"
                table_maker.field_names = ["ID", "Type", "Description"]
                table_maker.add_row([
                    "1", "The Additive Cipher", 'EnciphDeWord using addition'
                ])
                table_maker.add_row([
                    "2", "The Multiplicative Cipher",
                    "EnciphDeWord using multiplication"
                ])
                table_maker.add_row([
                    '3', 'The Affine Cipher',
                    'EnciphDeWord using combined of addition and multiplication'
                ])
                table_maker.add_row([
                    '4', 'The Hill Digraph Cipher',
                    'EnciphDeWord using 2x2 integer matricess'
                ])
                table_maker.add_row([
                    '5', 'The Hill Digraph Cipher',
                    'EnciphDeWord using 3x3 integer matrices'
                ])
                table_maker.add_row([
                    '6', 'The Vigenere Square (Cipher)',
                    'EnciphDeWord using word or string'
                ])

                print table_maker
        elif cmd_encrypt.startswith('use'):
            try:
                num_given = int(cmd_encrypt.split()[1])
                if num_given == 1:
                    loop_handler_one_key('additive', TheAdditiveCipher)
                elif num_given == 2:
                    loop_handler_one_key('multiplicative',
                                         TheMultiplicativeCipher)
                elif num_given == 3:
                    loop_handler_multi_key('affine', TheAffineCipher)
                elif num_given == 4:
                    loop_handler_multi_key('digraph', TheHillDigraphCipher)
                elif num_given == 5:
                    loop_handler_multi_key('trigraph', TheHillTrigraphCipher)
                elif num_given == 6:
                    loop_handler_word(TheVigenereSquare)
            except ValueError:
                print BERR + " Please specify ID." + BEND
                print "   ex. SET message 'your string'\n"
                pass
            except IndexError:
                print BERR + " Please specify ID.\n" + BEND
                print "Please type '{}show options{}' to show IDs".format(
                    CLIGHTBLUE, CEND)
                print "ex. use [id]\n"
                pass
        elif cmd_encrypt == 'back':
            break