def addSpaces(): if request.method == "POST": plainText = Format.remove(request.json["plain"], SPACE) plainText = SpaceAdd.addLongest(plainText) score = DetectEnglish.detectWord(plainText) * 100 return json.dumps({"plain": plainText, "score": f"{score}% certainty"}) return "error"
def reverseText(): args = {"title": "Reverse Text", "ciphText": "", "result": "", "score": 0} if request.method == "POST": args["ciphText"] = ciph = request.form["ciphInput"] args["result"] = plain = ciph[::-1] args["score"] = DetectEnglish.detectWord(SpaceAdd.addLongest(plain)) * 100 return render_template(f"tools/reversetext.html", **args)
def freqAnalysis(): args = {"title": "Frequency Analysis", "ciphText": "", "score": 0} if request.method == "POST": args["ciphText"] = ciph = request.form["ciphInput"] args["score"] = DetectEnglish.detectWord(SpaceAdd.addLongest(ciph)) * 100 plotFreq(args["ciphText"]) return render_template(f"tools/freqanalysis.html", **args)
def decrypt(ciph, keylen=0, key=""): """ Attempt decryption of the transposition-enciphered text. One of keylen or key is required to function. """ if not (key or keylen): return "", "" ciph = Format.keepOnly(ciph.lower(), ALPH, NUMS) text = _process(ciph, keylen=keylen, key=key) if key: return _decryptWithKey(text, key.split(",")) if keylen < 9: bestResult, bestKey = _decryptShortKey(text) else: bestResult, bestKey = _decryptLongKey(text, keylen) bestScore = DetectEnglish.detectWord(SpaceAdd.addLongest(bestResult)) text = _process(ciph, keylen=len(ciph) // keylen, key=key) text = "".join(text) text = _process(text, keylen=keylen, key=key) if keylen < 9: result, key = _decryptShortKey(text) else: result, key = _decryptLongKey(text, keylen) score = DetectEnglish.detectWord(SpaceAdd.addLongest(result)) if score > bestScore: bestResult = result bestKey = key overflow = len(ciph) % keylen if overflow != 0: bestScore = 0 lastset = bestResult[-overflow:] overflow = len(lastset) for perm in itertools.permutations(lastset, overflow): result = bestResult[:-overflow] + "".join(perm) score = DetectEnglish.detectWord(SpaceAdd.addLongest(result)) if score > bestScore: bestScore = score bestResult = result return bestResult, bestKey, bestScore
def caesar(): args = {"title": "Caesar", "ciphText": "", "result": "", "score": 0} if request.method == "POST": from Ciphers import Caesar ciphText = request.form["ciphInput"].lower() result, _ = Caesar.decrypt(ciphText) score = DetectEnglish.detectWord(SpaceAdd.addLongest(result)) * 100 args = {"title": "Caesar", "ciphText": ciphText, "result": result, "score": score} return render_template(f"ciphers/caesar.html", **args)
def transposition(): args = {"title": "Transposition", "ciphText": "", "result": "", "score": 0, "keylen": "", "key": ""} if request.method == "POST": from Ciphers import Transposition ciphText = Format.remove(request.form["ciphInput"], PUNC).lower() keylen = int(request.form["keylenInput"] or 0) key = request.form["keyInput"] result, key = Transposition.decrypt(ciphText, key=key, keylen=keylen) key = ",".join(key) score = DetectEnglish.detectWord(SpaceAdd.addLongest(result)) * 100 args = {"title": "Transposition", "ciphText": ciphText, "result": result, "score": score, "keylen": keylen, "key": key} return render_template(f"ciphers/transposition.html", **args)
def substitution(): args = {"title": "Substitution", "ciphText": "", "result": "", "score": 0, "vals": {}} if request.method == "POST": from Ciphers import Substitution ciphText = Format.remove(request.form["ciphInput"], PUNC).lower() if request.form.get("useSpace"): result, vals = Substitution.decryptWithSpaces(ciphText) else: result, vals = Substitution.decrypt(ciphText) score = DetectEnglish.detectWord(SpaceAdd.addLongest(result)) * 100 args = {"title": "Substitution", "ciphText": ciphText, "result": result, "score": score, "vals": vals} return render_template(f"ciphers/substitution.html", **args)
def vigenere(): args = {"title": "Vigenere", "ciphText": "", "result": "", "score": 0, "keylen": "", "key": ""} if request.method == "POST": from Ciphers import Vigenere ciphText = request.form["ciphInput"] keylen = request.form["keylenInput"] key = request.form["keyInput"] if request.form.get("oddAlph"): result = Vigenere.decryptWithSubstitution(ciphText) else: result, key, _ = Vigenere.decrypt(ciphText, key=key, keylen=keylen) score = DetectEnglish.detectWord(SpaceAdd.addLongest(result)) * 100 args = {"title": "Vigenere", "ciphText": ciphText, "result": result, "score": score, "keylen": keylen, "key": key} return render_template(f"ciphers/vigenere.html", **args)
def subInputs(): if request.method == "POST": changed = request.json["name"][0] newval = request.json["val"].lower() if newval == "": newval = "_" ciphText = Format.keepOnly(request.json["ciph"].lower(), ALPH) plainText = Format.remove(request.json["plain"], SPACE).lower() if plainText == "": new = ''.join([newval if x in changed else "_" for x in ciphText]) else: plainText = [x for x in plainText] for i, letter in enumerate(ciphText): if letter == changed: plainText[i] = newval new = "".join(plainText) score = DetectEnglish.detectWord(SpaceAdd.add(new)) * 100 return json.dumps({"plain": new, "score": f"{score}% certainty"}) return "error"