Personal Encryptor with Nonbreakable Inforation-theoretic Security
Description
Solution
def keygen(length):
key = ""
rnd_bytes = os.urandom(length)
for i in range(length):
pos = rnd_bytes[i] % len(ALPHABET)
key += ALPHABET[pos]
return key
...
def encrypt(key, msg):
assert len(key) == len(msg), "For Information-theoretic security the key needs to be as long as the msg."
ciphertext = ""
for i in range(len(msg)):
msg_c = msg[i]
key_c = key[i]
if msg_c not in ALPHABET:
ValueError(f"Can't encrypt char: {msg_c}")
msg_pos_c = ALPHABET.index(msg_c)
key_pos_c = ALPHABET.index(key_c)
new_pos = (msg_pos_c + key_pos_c) % len(ALPHABET)
ciphertext += ALPHABET[new_pos]
return ciphertext
Last updated