Skip to content

Instantly share code, notes, and snippets.

@iMel408
Created March 24, 2019 18:16
Show Gist options
  • Save iMel408/dca1b892832758b3c5b1e2f14a043407 to your computer and use it in GitHub Desktop.
Save iMel408/dca1b892832758b3c5b1e2f14a043407 to your computer and use it in GitHub Desktop.
basic string encryption using ord() and char() methods. based on excercise snippet found on @coderpedia ig.
text = 'meet me at the nut house'
def encrypt_str(txt):
"""
basic string encryption using:
ord() returns int representing Unicode for given char.
char() returns str representation in Unicode for given int.
"""
encrypted_txt = ''
for c in txt:
x = ord(c) # map char to unicode num
x = x + 1 # plus 1 to shift the mapping to next char in line
c2 = chr(x) # map unicode num (+1) back to char
encrypted_txt = encrypted_txt + c2 # create new string
return encrypted_txt
encrypted_str = encrypt_str(text)
print("Encrypting...")
print("Encrypted: ",encrypted_str)
def decrypt_str(encrypted_str):
decrypted_txt = ''
for c in encrypted_str:
x = ord(c)
x = x - 1
c2 = chr(x)
decrypted_txt = decrypted_txt + c2
return decrypted_txt
print("Decrypted: ",decrypt_str(encrypted_str))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment