Created
March 24, 2019 18:16
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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