From e6305c4b0086161768c7f2d7e104107a82f04e4a Mon Sep 17 00:00:00 2001 From: Leon Vatthauer Date: Wed, 16 Oct 2024 11:09:46 +0200 Subject: [PATCH] add caesar and masc encryption --- caesar.py | 2 ++ masc.py | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 caesar.py create mode 100644 masc.py diff --git a/caesar.py b/caesar.py new file mode 100644 index 0000000..efc1ac6 --- /dev/null +++ b/caesar.py @@ -0,0 +1,2 @@ +def caesar(k : int, plain : str) -> str: + return str([chr(ord(c) + k) for c in plain]) diff --git a/masc.py b/masc.py new file mode 100644 index 0000000..bc99564 --- /dev/null +++ b/masc.py @@ -0,0 +1,23 @@ +default_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + +def masc(k : str, plain : str, alphabet : str = default_alphabet): + # make sure that k only contains characters from alphabet + for c in k: + if c not in alphabet: + raise ValueError("key contains character thats not in alphabet, namely: " + c) + + # build key + ## make unique + k_unique = [] + for c in k: + if not (c in k_unique): + k_unique.append(c) + + ## append alphabet + last = k_unique[len(k_unique) - 1] + alphabet_pos = alphabet.index(last) + for i in list(range(alphabet_pos + 1, len(alphabet))) + list(range(0, alphabet_pos)): + if alphabet[i] not in k_unique: + k_unique.append(alphabet[i]) + + return k_unique