def getBest(possKeys, ciph): """Find best mapping in given possibilities.""" results = [] for key in possKeys: keyMap = dict(zip(ALPH, key)) result = sub(ciph, keyMap) results.append((key, DetectEnglish.detect(result), -DetectEnglish.chiSquared(result))) best = max(results) bestMap = dict(zip(ALPH, best[0])) result = sub(ciph, bestMap) return result, best[1], bestMap
def decrypt(ciph): """Try every possible shift and choose the best result.""" bestScore = 9e99 bestKey = "" bestResult = "" for i in range(26): result = shift(ciph, i) score = DetectEnglish.chiSquared(result) if score < bestScore: bestScore = score bestResult = result bestKey = ALPH[i] return bestResult, bestKey