Пример #1
0
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"
Пример #2
0
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)
Пример #3
0
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)
Пример #4
0
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"
Пример #5
0
def decryptWithSpaces(ciph, keyMap=""):
    """
    Use pattern-matching techniques to decipher a substituted alphabet.

    Requires properly spaced ciphertext to be effective.
    """

    if not keyMap:
        keyMap = {key: set(ALPH) for key in ALPH}

    ciph = Format.remove(ciph, PUNC).lower()
    if not ciph:
        return ciph, {x: "" for x in ALPH}

    with open("static/txt/patterns.json", encoding="utf-8") as f:
        patterns = json.load(f)

    # Reformat text into set
    for cw in set(ciph.split(" ")):

        newMap = {key: set() for key in ALPH}

        # Match pattern to wordlist
        pattern = PatternGen.pattern(cw)
        if pattern in patterns:
            for word in patterns[pattern]:
                for i, letter in enumerate(cw):
                    newMap[letter].add(word[i])

            # Remove impossible letters
            for letter in set(cw):
                keyMap[letter] = keyMap[letter] & newMap[letter] if keyMap[
                    letter] & newMap[letter] else keyMap[letter]

    solved = set()
    while True:

        # Look for 1-length (solved) mappings
        oldSolved = set(solved)
        solved = set(
            next(iter(val)) for val in keyMap.values() if len(val) == 1)

        if oldSolved == solved:
            break

        # Remove solved letters from other possible mappings
        _removeSolved(keyMap, solved)

    keyMap = {letter: keyMap[letter] or {"_"} for letter in keyMap}

    keylens = {length: [] for length in map(len, keyMap.values())}
    for letter in keyMap:
        keylens[len(keyMap[letter])].append(letter)

    while True:
        if len(keylens) == 1:
            break

        poss = []
        for letter in ALPH:
            if letter in keylens[1] + keylens[list(keylens)[1]]:
                poss.append(keyMap[letter])
            else:
                poss.append({"_"})
        possKeys = list("".join(x) for x in itertools.product(*poss))
        _, _, bestMap = getBest(possKeys, ciph)
        for k, v in bestMap.items():
            if len(v) == 1 and v != "_":
                keyMap[k] = {v}

        while True:

            # Look for 1-length (solved) mappings
            oldSolved = set(solved)
            solved = set(
                next(iter(val)) for val in keyMap.values() if len(val) == 1)

            if oldSolved == solved:
                break

            # Remove solved letters from other possible mappings
            _removeSolved(keyMap, solved)

        keylens = {length: [] for length in map(len, keyMap.values())}
        for letter in keyMap:
            keylens[len(keyMap[letter])].append(letter)

    return sub(ciph, keyMap), keyMap