In light of this months Community Challenge, I've taken a stab at building something related. Keep in mind, this isn't a submission for the challenge, just a utility to help in the later process of completing the challenge.
What we have here is your simple encryption and decryption (with a given key) tool. I'd like to know if what I have is the most efficient, the most well written, and the simplest form of these functions.
import random
alpha = "abcdefghijklmnopqrstuvwxyz"
def encrypt(original, key=None):
if key is None:
l = list(alpha)
random.shuffle(l)
key = "".join(l)
new = []
for letter in original:
new.append(key[alpha.index(letter)])
return ["".join(new), key]
def decrypt(cipher, key=None):
if key is not None:
new = []
for letter in cipher:
new.append(alpha[key.index(letter)])
return "".join(new)
# we'll have an else here for the actual Community Challenge!
I'm nowhere near an expert with Python, so if I'm not following coding standards, please guide me there too.
A simple test might be:
e = encrypt("helloworld", None)
print(e)
print(decrypt(e[0], e[1]))
and we'd get something along the lines of:
['wussndnkst', 'qxvtupaweroslynzhkbigcdjmf']
helloworld