Skip to content
Sign upLog in
← Back to Community

encrypt and decrypt

Profile icon
kingTalent

function called “getEncryptedText()” that takes a random string and generates a
password out of it and another function called “getDecryptedText()” that takes the output of the
“getEncryptedText()” function and returns the original string

Voters
Profile icon
alfredobustos
Profile icon
kingTalent
Comments
hotnewtop
Profile icon
OwenBradstreet

Do you want this made, or have you made it?

Profile icon
OwenBradstreet

@OwenBradstreet
Here's what I came up with:
def encode(clear, key):
enc = []
for i in range(len(clear)):
keyC = key[i % len(key)]
encC = chr((ord(clear[i]) + ord(keyC)) % 256)
enc.append(encC)
return base64.urlsafe_b64encode("".join(enc).encode()).decode()

def decode(enc, key):
dec = []
enc = base64.urlsafe_b64decode(enc).decode()
for i in range(len(enc)):
keyC = key[i % len(key)]
dec_c = chr((256 + ord(enc[i]) - ord(keyC)) % 256)
dec.append(dec_c)
return "".join(dec)