Reputation: 1547
So I wrote a little script in python that brings up a gui with 2 buttons and a text field. you type into the text area and click encrypt and the text all of a sudden looks crazy. coooooool.
but then when you hit decrypt, it doesn't go back to the original. here is the code
from Tkinter import *
import ttk
root = Tk()
textArea = Text(root, state = "normal")
def encrypt():
message = textArea.get('1.0', 'end')
newMessage = ''
lastChar = ''
for c in message:
if lastChar != '':
newMessage += chr((ord(c) + ord(lastChar)) % 256)
lastChar = chr((ord(c) + ord(lastChar)) % 256)
else:
newMessage += chr((ord(c) + 5) % 256)
lastChar = chr((ord(c) + 5) % 256)
textArea.delete('1.0', 'end')
textArea.insert('end', newMessage)
def decrypt():
message = textArea.get('1.0', 'end')
newMessage = ''
lastChar = ''
for c in message:
if lastChar != '':
newMessage += chr((ord(c) - ord(lastChar)) % 256)
lastChar = chr((ord(c) - ord(lastChar)) % 256)
else:
newMessage += chr((ord(c) - 5) % 256)
lastChar = chr((ord(c) - 5) % 256)
textArea.delete('1.0', 'end')
textArea.insert('end', newMessage)
encrypt = ttk.Button(root, text = "Encrypt", command = encrypt)
encrypt.pack({"side": "top"})
decrypt = ttk.Button(root, text = "Decrypt", command = decrypt)
decrypt.pack({"side": "top"})
textArea.pack({"side": "bottom"});
mainloop()
the problem is that it doesn't show the original text. It just seems to make it more cryptic. what is wrong here? Please help.
update: a changed it so it just adds 5. and It works. So that tells me that it's the part where I add last characters code value. There is still one problem: it adds a new line and this strange line character (not the pipe ---> |).
now the code is fixed. thanks. here is the result: WW1ueCVueCV1d2p5eX4laHR0cTMlWW1mc3AlfnR6JXh5ZmhwJXR7andrcXR8JWt0dyV5bWolZnN4fGp3Mw8=
after I made the decrypt so It doesn't take away five, it worked. Thanks again.
Upvotes: 1
Views: 328
Reputation: 43495
This seems a rather trivial "encryption" to break. Everyone who has the source can read your messages. A secure cryptosystem should not depend on the algorithm being secure.
You could use the Python Cryptography Toolkit (www.dlitz.net/software/pycrypto/). It has a host of different encryption algorithms available. You can find some examples of how to use it at: http://www.laurentluce.com/posts/python-and-cryptography-with-pycrypto/
If you don't care about security, why not use e.g. one of the standard encodings (see §7.8 'codec' of the standard library reference);
The Ceasar cipher:
>>> res = 'this is a test'.encode('rot_13')
>>> print res
guvf vf n grfg
>>> print res.decode('rot_13')
this is a test
Base64 encoding:
>>> res = 'this is a test'.encode('base64')
>>> print res
dGhpcyBpcyBhIHRlc3Q=
>>> print res.decode('base64')
this is a test
Upvotes: 2
Reputation: 98469
You can't put arbitrary bytes into a text area.
Your encryption algorithm is purely bytewise - it works on the numeric values of the characters. This isn't going to work, even for ASCII, because byte 0 is an ASCII NUL
- treated specially, and byte 127 (what 'Z', character 122, becomes when shifted up by five) is a DEL
character: not what you want! Moving beyond ASCII, the real world's UTF-8 has thousands of invalid byte sequences.
If you're going to be using bytewise encryption like this, you can't put the results into a text field - it's binary data. If you want it to be able to go into a text field, you must make sure it's valid text without control characters, NUL
bytes, or invalid byte sequences.
One easy way of making the binary sanitary is to Base64-encode it on encryption, and have the first step of decryption be Base64 decoding.
Final note: you're also encrypting the newline at the end of the text area. Probably not something you wish to do.
Upvotes: 5