add caesar and masc encryption

This commit is contained in:
Leon Vatthauer 2024-10-16 11:09:46 +02:00
commit e6305c4b00
Signed by: leonv
SSH key fingerprint: SHA256:G4+ddwoZmhLPRB1agvXzZMXIzkVJ36dUYZXf5NxT+u8
2 changed files with 25 additions and 0 deletions

2
caesar.py Normal file
View file

@ -0,0 +1,2 @@
def caesar(k : int, plain : str) -> str:
return str([chr(ord(c) + k) for c in plain])

23
masc.py Normal file
View file

@ -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