Last active
September 15, 2019 03:47
-
-
Save Ra1d7/6efe65da735d49169afb253dc9997d7b to your computer and use it in GitHub Desktop.
Simple Encryption and Decryption Using a dictionary and keys
This file contains hidden or 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
chars = { | |
'!':-15, | |
'"':-14, | |
'=':-13, | |
'+':-12, | |
'-':-11, | |
'*':-10, | |
'#':-9, | |
'$':-8, | |
'%':-7, | |
'5':-6, | |
'4':-5, | |
'3':-4, | |
'2':-3, | |
'1':-2, | |
'0':-1, | |
'@':0, | |
'a':1, | |
'b':2, | |
'c':3, | |
'd':4, | |
'e':5, | |
'f':6, | |
'g':7, | |
'h':8, | |
'i':9, | |
'j':10, | |
'k':11, | |
'l':12, | |
'm':13, | |
'n':14, | |
'o':15, | |
'p':16, | |
'q':17, | |
'r':18, | |
's':19, | |
't':20, | |
'u':21, | |
'v':22, | |
'w':23, | |
'x':24, | |
'y':25, | |
'z':26, | |
'6':27, | |
'7':28, | |
'8':29, | |
'9':30, | |
' ':31, | |
'^':32, | |
'~':33, | |
'(':34, | |
')':35, | |
'?':36, | |
"\\":37, | |
'&':38, | |
'.':39 | |
} | |
inv = {} | |
for key, val in chars.items(): | |
inv[val] = inv.get(val, []) + [key] | |
option = input('(E)ncrypt / (D)ecrypt?: ').lower() | |
word = input('Text: ').lower() | |
kay = input('Key: ') | |
kay = int(kay) / int(len(kay)) | |
kay = str(kay) | |
kay = int(kay[0]) | |
def encrypt(word): | |
global kay | |
global chars | |
global inv | |
myencstring ='' | |
for char in word: | |
ore = chars[char] | |
ore -= kay | |
cher = str(inv[ore]).replace('[\'','') | |
cher = cher.replace('\']','') | |
myencstring += cher | |
return myencstring | |
def decrypt(word): | |
global kay | |
global chars | |
global inv | |
mydecryptedstring ='' | |
for char in word: | |
ore = chars[char] | |
ore += kay | |
cher = str(inv[ore]).replace('[\'','') | |
cher = cher.replace('\']','') | |
mydecryptedstring += cher | |
return mydecryptedstring | |
if 'e' in option: | |
print('Your Encrypted String: \033[31m'+encrypt(word)+'\033[0m') | |
elif 'd' in option: | |
print('Your Decrypted String: \033[32m'+decrypt(word)+'\033[0m') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NOTE: This only "encrypts" characters and numbers, it will output a error if there is a symbol in the text you're trying to encrypt