Double Dice Crypto

I heard you like double dice crypto to solve a mystery? Feel free to use my quickly hacked python script below or entrust your data to a third party.

Hint You can automate the process much better if you work locally 😏

#!/usr/bin/env python3

print("Roll the dice, Santa!")

def encrypt(plain, key):
    cipher = list(key)
    for i in range(len(cipher)):
        cipher[i] += str(i)
    for i in range(len(plain)):
        cipher[i % len(key)] += plain[i]
    cipher = sorted(cipher)
    for i in range(len(cipher)):
        cipher[i] = cipher[i][2:]
    return "".join(str(e) for e in cipher)

def decrypt(cipher, key):
    rkey = sorted(key)
    plain = []
    ret = ""
    for i in range(len(key)):
        plain.append({ "pos": rkey.index(key[i]),
                       "len": len(cipher) // len(key),
                       "txt": "" })
        rkey[rkey.index(key[i])] = ' '
    for i in range(len(cipher) % len(key)):
        plain[i]["len"] += 1
    j = 0
    for i in range(len(key)):
        for p in plain:
            if p["pos"] == i:
                break
        while len(p["txt"]) < p["len"]:
            p["txt"] += cipher[j]
            j += 1
    i = 0
    while i < len(cipher):
        ret += plain[i % len(key)]["txt"][i // len(key)]
        i += 1
    return ret

# PoC
# Example from http://public.hochschule-trier.de/~wambacht/webseite2/

cipher="NRSGSESAIEOZRABINADIILURTNDEHXUSRHEVIEEPAEHEEGTLZFTLIANMEL"
key1="NOTEBOOK"
key2="DECKEL"

print(decrypt(decrypt(cipher, key2), key1))