def hackAffine(message): print('Hacking...') # Python programs can be stopped at any time by pressing Ctrl-C (on # Windows) or Ctrl-D (on Mac and Linux) print('(Press Ctrl-C or Ctrl-D to quit at any time.)') # brute-force by looping through every possible key for i in range(26 ** 2): key = 0 while True: key = random.randint(2, 1024) if crackMath.gcd(key, 26) == 1: break for j in range(1,26): decryptedText = affineCipher.decrypt(message,key,j) if not SILENT_MODE: print 'Tried Key %s, %s... (%s)' % (key, j, decryptedText[:40]) if isEnglish.method(decryptedText)>70: # Check with the user if the decrypted key has been found. print print'Possible encryption hack:' print'Key: %s, %s' % (key, j) print'Decrypted message: ' + decryptedText[:200] print print'Enter D for done, or just press Enter to continue hacking:' response = raw_input('> ') if response.strip().upper().startswith('D'): return decryptedText else: continue return None
def checkForEnglish(text, number, percent): """text is the text we are checking, number is the number of words already transformed. percent is the percent that is desired to be english. note that if 70% is the percent, it will fail whenever the first n = number of words are only 70% english Basically this is the UI for this program, If it fails, it will return True If it is english, it'll ask, if it seems like english to the user, then the system will exit else, it just returns False. """ # first, we simply say False if the number is too small if number < 2: return False # next, we split up the words temp = [i for i in text if i in ALPHA] temp = "".join(temp) words = temp.split(" ") words = words[:number] sentence = " ".join(words) # next we check for fail failCheck = isEnglish.method(sentence) failPercent = 100 - percent # print round(failCheck), text[:60] if failCheck < 50: return True # next we check to see if it is English check = isEnglish.method(text) if check >= percent: print "hmm... possible crack. does this look right?" print text answer = raw_input("y/n: ") if answer == "y": exit() else: return True # the default case, is basically False, keep on cracking return False
def method(text, percent = 70): for i in range(2,len(text)): possible = td.decrypt(text,i) poss_percent = isEnglish.method(possible) if poss_percent > percent: print "possible solution: " print possible print "key: " , i print "do you want to keep going? (y/n): " answer = raw_input(">>>") if answer == "n": break