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
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
def main(): global encrypt banner_show() help_ = helper.help_('options', 'none') print help_.format(CGREEN, CEND, CWHITE) while True: try: cmd = int( raw_input("{}Enter{} (digit only)>> ".format(BFAIL, BEND))) if cmd == 1: print BOKMSG + " Switching to encrypt..." + BEND time.sleep(0.7) encrypt.handler() elif cmd == 2: print BOKMSG + " Switching to decrypt..." + BEND time.sleep(0.7) decrypt.handler() elif cmd == 3: print BOKMSG + " Switching to cipher Analysis..." + BEND time.sleep(0.7) cipherAnalysis.handler() elif cmd == 4: help_ = helper.help_('options', 'none') print help_.format(CGREEN, CEND, CWHITE) elif cmd == 5: help_ = helper.help_('author', 'none') print help_.format(CGREEN, CEND, CRED, CLIGHTBLUE, CUNDERLINE, CWHITE) elif cmd == 6: banner_show() elif cmd == 7: print "\n\n" + BOKMSG + " GOOD BYE!!!" + BEND break except ValueError as e: error_command = e.message.split()[7] print BERR + " Unknown command >> {}{}{}\n".format( CGREEN, error_command, CEND) + BEND print "type {}'3'{} for options".format(CGREEN, CEND) except KeyboardInterrupt: print "\n\n" + BOKMSG + " GOOD BYE!!!" + BEND break
def loop_handler_word(function): message = '' result = '' key = '' while True: comd = raw_input("{0}{1}(Decrypt/{2}{3}{4}){5} >> ".format( BUNDERLINE, BBLUE, CRED, 'The Vigenere Square', CEND, BEND)).strip() if comd == 'help' or comd == 'h' or comd == '?': help_ = helper.help_('decrypt', 'in') print help_.format(CLIGHTBLUE, CEND) elif comd == 'profile': info = function.info() print info.format(CLIGHTBLUE, CEND) elif comd.startswith('show') and comd.endswith('info'): info_box = Table_maker(message, key, result, 'decrypt') print print info_box print elif comd.startswith('SET') or comd.startswith('set'): try: msgOrkey = comd.split()[1] strOrnum = comd.rsplit()[2:] if msgOrkey == 'message': message = strOrnum[0] print BOKMSG + " Changing encrypted message to >>{} {}\n".format( BEND, message) elif msgOrkey == 'key': key = strOrnum[0] print BOKMSG + " Changing encrypted key to >>{} {}\n".format( BEND, key) except IndexError: print BERR + " Please specify (message/key) [string/(word or string)]" + BEND print " ex. SET message 'your string'\n" elif comd == 'message' or comd == 'msg': try: message = raw_input("{}Enter your encrypted string:{} ".format( CGREEN, CEND)).strip() print BOKMSG + " Changing encrypted message to >>{} {}\n".format( BEND, message) for letter in message: if letter.isdigit(): raise ValueError() elif message == '': raise ValueError() except ValueError: print BERR + " Please don't use digit in your string or none\n" + BEND elif comd == 'key': try: key = raw_input( "{}What string or word did you use as key:{} ".format( CGREEN, CEND)).strip() if key.isdigit(): raise ValueError() print BOKMSG + " Changing encrypted key to >>{} {}\n".format( BEND, key) except ValueError: print BERR + " Please only use alphabet as key and not allow spaces\n" + BEND elif comd == 'execute': try: if message == '' or key == '': raise ValueError() else: result = function.Decrypt(message, key) if result == '': print "\n" + BERR + ' Your decrypted message is >>{} {}'.format( BEND, result) break print "\n" + BOKMSG + ' Your decrypted message is >>{} {}'.format( BEND, result) except ValueError: print BERR + " Please set your message/key\n" + BEND elif comd == 'back': break
def loop_handler_multi_key(type, function): Types = { 'affine': 'The Affine Cipher', 'digraph': 'The Hill Digraph Cipher', 'trigraph': 'The Hill Trigraph Cipher' } keys_ID = [ "Key A", "Key B", "Key C", "Key D", "Key E", "Key F", "Key G", "Key H", "Key I", "None" ] name = [value for key, value in Types.iteritems() if key == type] message = '' result = '' keys = [0 * i for i in range(10)] if type == 'affine': for i in range(8): keys.pop() keys_ID.pop() elif type == 'digraph': for i in range(6): keys.pop() keys_ID.pop() elif type == 'trigraph': keys.pop() keys_ID.pop() while True: comd = raw_input("{0}{1}(Decrypt/{2}{3}{4}){5} >> ".format( BUNDERLINE, BBLUE, CRED, name[0], CEND, BEND)).strip() if comd == 'help' or comd == 'h' or comd == '?': help_ = helper.help_('decrypt', 'in') print help_.format(CLIGHTBLUE, CEND) elif comd == 'profile': info = function.info() print info.format(CLIGHTBLUE, CEND) elif comd.startswith('show') and comd.endswith('info'): info_box = Table_maker_multi(message, keys, result, 'decrypt', keys_ID, type) print print info_box print elif comd.startswith('SET') or comd.startswith('set'): try: msgOrkey = comd.split()[1] strOrnum = comd.rsplit()[2:] if msgOrkey == 'message': message = strOrnum[0] print BOKMSG + " Changing encrypted message to >>{} {}\n".format( BEND, message) elif msgOrkey == 'key': for i in range(len(keys)): keys[i] = int(strOrnum[i].strip()) print BOKMSG + " Changing {} to >>{} {}".format( keys_ID[i], BEND, keys[i]) time.sleep(0.5) except IndexError: print BERR + " Please specify (message/key(A/B)) [string/number(with space between them)]" + BEND print " ex. SET message 'your encrypted string'\n" print " ex. SET key [keys in order {} with spaces] <== according to '{}show info{}'\n".format( keys_ID, CLIGHTBLUE, CEND) elif comd == 'message' or comd == 'msg': try: message = raw_input("{}Enter your encrypted string:{} ".format( CGREEN, CEND)).strip() print BOKMSG + " Changing encrypted message to >>{} {}\n".format( BEND, message) for letter in message: if letter.isdigit(): raise ValueError() elif message == '': raise ValueError() except ValueError: print BERR + " Please don't use digit in your string or none\n" + BEND elif comd == 'key': try: for i in range(len(keys)): keys[i] = int( raw_input( "{}How many times did you encrypted for {}:{} ". format(CGREEN, keys_ID[i], CEND)).strip()) print BOKMSG + " Changing {} to >>{} {}\n".format( keys_ID[i], BEND, keys[i]) except ValueError: print BERR + " Foudn spaces in keys\n" + BEND elif comd == 'execute': try: if message == '' or keys[0] == 0: raise ValueError() else: result = function.Decrypt(message, keys) if not result: pass else: print "\n" + BOKMSG + ' Your encrypted message is >>{} {}'.format( BEND, result) except ValueError: print BERR + " Please set your message/key\n" + BEND elif comd == 'back': break
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
def loop_handler_one_key(type, function): Types = { 'additive': 'The Additive Cipher', 'multiplicative': 'The Multiplicative Cipher' } name = [value for key, value in Types.iteritems() if key == type] message = '' result = '' key_num = 0 while True: comd = raw_input("{0}{1}(Decrypt/{2}{3}{4}){5} >> ".format( BUNDERLINE, BBLUE, CRED, name[0], CEND, BEND)).strip() if comd == 'help' or comd == 'h' or comd == '?': help_ = helper.help_('decrypt', 'in') print help_.format(CLIGHTBLUE, CEND) elif comd == 'profile': info = function.info() print info.format(CLIGHTBLUE, CEND) elif comd.startswith('show') and comd.endswith('info'): info_box = Table_maker(message, key_num, result, 'decrypt') print print info_box print elif comd.startswith('SET') or comd.startswith('set'): try: msgOrkey = comd.split()[1] strOrnum = comd.rsplit()[2:] if msgOrkey == 'message': message = strOrnum[0] print BOKMSG + " Changing encrypted message to >>{} {}\n".format( BEND, message) elif msgOrkey == 'key': key_num = int(strOrnum[0]) print BOKMSG + " Changing key to >>{} {}\n".format( BEND, key_num) except IndexError: print BERR + " Please specify (message/key) [string/number]" + BEND print " ex. SET message 'your string'\n" elif comd == 'message' or comd == 'msg': try: message = raw_input( "{}Enter your encrypted message:{} ".format(CGREEN, CEND)).strip() print BOKMSG + " Changing message to >>{} {}\n".format( BEND, message) for letter in message: if letter.isdigit(): raise ValueError() elif message == '': raise ValueError() except ValueError: print BERR + " Found digits and none in encrypted message\n" + BEND elif comd == 'key': try: key_num = int( raw_input("{}How many times did you encrypted:{} ".format( CGREEN, CEND)).strip()) print BOKMSG + " Changing key to >>{} {}\n".format( BEND, key_num) except ValueError: print BERR + " Found spaces in key\n" + BEND elif comd == 'execute': try: if message == '' or key_num == 0: raise ValueError() else: result = function.Decrypt(message, key_num) if result == '': print "\n" + BERR + ' Your decrypted message is >>{} {}'.format( BEND, result) break print "\n" + BOKMSG + ' Your decrypted message is >>{} {}'.format( BEND, result) except ValueError: print BERR + " Please set your message/key\n" + BEND elif comd == 'back': break