def hackVigenere(ciphertext): wordNumber = 1 fo = open("C:\\Users\\theka\\Desktop\\Crypto\\Input\\dictionary.txt") words = fo.readlines() fo.close() print("Attempting dictionary decryption") for word in words: print(str(wordNumber)) word = word.strip() decryptedText = vigenere.decryptMessage(word, ciphertext) if detectEnglish.isEnglish(decryptedText, wordPercentage=40): print() print("Possible encryption break:") print() print("key" + str(word) + ": " + decryptedText[:100]) print("Enter " "D" " for done, or hit enter to continue") response = input("> ") if response.upper().startswith("D"): return decryptedText wordNumber += 1
def getTranslatedMessage(mode, cipher, message, key = None): if key != None: if mode[0] == 'd' and cipher[0] == 'c': key = -key if cipher[0] == 'c': return caesar.caesar.caesar_cipher(message, key) else: if mode[0] == 'e': return vigenere.encryptMessage(key, message) else: return vigenere.decryptMessage(key, message) else: return reverse.reverse.reverse_cipher(message)
def getTranslatedMessage(mode, cipher, message, key): if mode[0] == 'd' and cipher[0] == 'c': key = -key translated = '' if cipher[0] == 'c': translated = caesar.caesar.caesar_cipher(message, key) else: if mode[0] == 'e': translated = vigenere.encryptMessage(key, message) else: translated = vigenere.decryptMessage(key, message) return translated
def attemptHackWithKeyLength(ciphertext, mostLikelyKeyLength): # determines the most likely letters for each letter in the key. ciphertextUp = ciphertext.upper() # allFreqScores is a list of mostLikelyKeyLength number of lists. # These inner lists are the freqScores lists. allFreqScores = [] for nth in range(1, mostLikelyKeyLength + 1): nthLetters = getNthSubkeyLetters(nth, mostLikelyKeyLength, ciphertextUp) # freqScores is a list of tuples # List is sorted by match score. Higher score means better match. freqScores = [] for possibleKey in LETTERS: decryptText = vigenere.decryptMessage(possibleKey, nthLetters) keyAndFreqMatchTuple = ( possibleKey, frequencyFinder.englishFreqMatchScore(decryptText)) freqScores.append(keyAndFreqMatchTuple) # Sort by match score freqScores.sort(key=getItemAtIndexOne, reverse=True) allFreqScores.append(freqScores[:NUM_MOST_FREQ_LETTERS]) if not SILENT_MODE: for i in range(len(allFreqScores)): print("Possible letters for letter %s of the key: " % (i + 1), end="") for freqScore in allFreqScores[i]: print("%s " % freqScore[0], end="") print() # Try every combination of the most likely letters for each position in the key. for indexes in itertools.product(range(NUM_MOST_FREQ_LETTERS), repeat=mostLikelyKeyLength): # Create a possible key from the letters in allFreqScores possibleKey = '' for i in range(mostLikelyKeyLength): possibleKey += allFreqScores[i][indexes[i]][0] if not SILENT_MODE: print('Attempting with key: %s' % (possibleKey)) decryptedText = vigenere.decryptMessage(possibleKey, ciphertextUp) if detectEnglish.isEnglish(decryptedText): # Set the hacked cipher text to the original casing origCase = [] for i in range(len(ciphertext)): if ciphertext[i].isupper(): origCase.append(decryptedText[i].upper()) else: origCase.append(decryptedText[i].lower()) decryptedText = "".join(origCase) # Check with user to see if the key has been found. print("Possible encryption hack with key %s:" % (possibleKey)) print(decryptedText[:200]) # only show first 200 characters print() print("Enter D for done, or just press Enter to continue hacking:") response = input("> ") if response.strip().upper().startswith("D"): return decryptedText # No English-looking decryption found, so return None. return None