def test(): chiList = getKeyChiSquaredStatistics(textoperations.getFileAsString("duan.encrypt"), getPTableAlphabet("sweletterfrequency.txt"), 11) for key in chiList: print("Key for index: ", key[0]) temp = getNMinMax(key[1], 3, False) print("Likley keys:", temp) print("Corresponds to the character:", alphabet[key[1].index(temp[1])])
def getTAfileStringsList(filePath): cFiles = [] for file in textoperations.getFilesInFolder(filePath): if(file[-12:-8] == "text"): cFiles.append(file) cipherStrings = [] for fileName in cFiles: cipherStrings.append(textoperations.getFileAsString(fileName)) return cipherStrings
def getPTableAlphabet(fileName): """Reads a file where every row have the letter and occurence as a percentage seperated by |""" alphF = textoperations.getFileAsString(fileName).split('\n') i = 0 for dataString in alphF: dataString = dataString.split('|') f = 0.0 f = float(dataString[1]) alphF[i] = [dataString[0], f] i += 1 return alphF
def doCrypt(inFile, keyFile, outFile, encryptB): if(os.path.exists(inFile) and os.path.exists(keyFile)): if(encryptB): inString = textoperations.getFormattedStringFromFile(inFile) else: inString = textoperations.getFileAsString(inFile) keyString = textoperations.getFormattedStringFromFile(keyFile) outString = "" if(encryptB): outString = encrypt(inString, keyString) else: outString = decrypt(inString, keyString) textoperations.printOutputToFile(outFile, outString) else: print("Didn't find the keyfile or the inFile file.")
def getICforKeyLengths(start, stop, fileName): cipherText = textoperations.getFileAsString(fileName) answers = [] for i in range(start, stop+1): answers.append([i, 0]) for i in range(start, stop+1): answers[i-start][1] = getIndexOfCoincidence(cipherText, i) maximum = 0.0 maxKey = 0 sKey = 0 sMax = 0.0 for keyIC in answers: if(keyIC[1]>maximum): sKey = maxKey sMax = maximum maximum = keyIC[1] maxKey = keyIC[0] print("Maximum IC found is: ", maximum, " for key of length ", maxKey) print("Second largest pair is (", sKey, ", ", sMax, ")") return answers
def getKeyCharCandidates(cipherFile, frequencyFile, keyLength, nrOfCandidates): return getKeyCharCandidates_(textoperations.getFileAsString(cipherFile), frequencyFile, keyLength, nrOfCandidates)
def freidmanTest(encryptedTextFile, minKey, maxKey): return friedmanTest_(textoperations.getFileAsString(encryptedTextFile), minKey, maxKey)