I'm currently making a simple tool that encrypts/decrpyts text with some code found here: https://stackoverflow.com/a/16321853/4285156
It's been working perfectly fine when I run it in CMD. The problem came up when I was trying to use the code together with Tkinter Text() widgets.
Here's a screenshot of my program: https://i.stack.imgur.com/aoNV1.png
As you can see, it encrypts the text "Hello, world." just fine. The problems is that whenever I try to decrypt something, the console gives me an error message and it won't work.
I'm thinking it has to do with the fact that I'm using Text() widgets, but why does it work when I encrypt stuff, but not when trying to decrypt things? As I said earlier, it works fine both ways when I'm not using Tkinter.
Here's the code:
from Tkinter import *
import base64
password = "aS4KmfkKr5662LfLKjrI6Kan4fK"
def encode(key, clear):
enc = []
for i in range(len(clear)):
key_c = key[i % len(key)]
enc_c = chr((ord(clear[i]) + ord(key_c)) % 256)
enc.append(enc_c)
return base64.urlsafe_b64encode("".join(enc))
def decode(key, enc):
dec = []
enc = base64.urlsafe_b64decode(enc)
for i in range(len(enc)):
key_c = key[i % len(key)]
dec_c = chr((256 + ord(enc[i]) - ord(key_c)) % 256)
dec.append(dec_c)
return "".join(dec)
GUI = Tk()
GUI.title('Cryppy')
GUI.minsize(width=300, height=300)
txt1 = Label(GUI, text="Decrypted Text")
txt1.grid(row=0, column=0, sticky=W, padx=(10,10))
tBox1 = Text(GUI, bd=2, width=60, height=5)
tBox1.grid(row=1, column=0, sticky=W, padx=(10,10))
txt2 = Label(GUI, text="Encrypted Text:")
txt2.grid(row=2, column=0, sticky=W, padx=(10,10))
tBox2 = Text(GUI, bd=2, width=60, height=5)
tBox2.grid(row=3, column=0, sticky=W, padx=(10,10))
def cryptIt():
codeIt = encode(password, tBox1.get("1.0", "end-1c"))
print codeIt
tBox2.delete("1.0", END)
tBox2.insert(END, codeIt)
def decryptIt():
decodeIt = decode(password, tBox2.get("1.0", "end-1c"))
print decodeIt
tBox1.delete("1.0", END)
tBox1.insert(END, decodeIt)
bCrypt = Button(GUI, text="Encrypt", command=cryptIt)
bCrypt.grid(row=4, sticky=W, padx=(10,10))
bCrypt2 = Button(GUI, text="Decrypt", command=decryptIt)
bCrypt2.grid(row=4, sticky=W, padx=(65,10))
GUI.mainloop()
And here is the error message
Exception in Tkinter callback
Traceback (most recent call last):
File "E:\Python27\lib\lib-tk\Tkinter.py", line 1486, in __call__
return self.func(*args)
File "C:\Users\Runar\Google Drive\Prosjekter\SyncNote\Source Code\mainEx.py", line 47, in decryptIt
decodeIt = decode(password, tBox2.get("1.0", "end-1c"))
File "C:\Users\Runar\Google Drive\Prosjekter\SyncNote\Source Code\mainEx.py", line 16, in decode
enc = base64.urlsafe_b64decode(enc)
File "E:\Python27\lib\base64.py", line 112, in urlsafe_b64decode
return b64decode(s, '-_')
File "E:\Python27\lib\base64.py", line 71, in b64decode
s = _translate(s, {altchars[0]: '+', altchars[1]: '/'})
File "E:\Python27\lib\base64.py", line 36, in _translate
return s.translate(''.join(translation))
TypeError: character mapping must return integer, None or unicode