我的 python 加密软件无法运行
所以我用 python 编写了一个小脚本,它弹出一个带有 2 个按钮和一个文本字段的 gui。您在文本区域中键入内容并单击“加密”,文本突然看起来很疯狂。酷酷。
但是当你点击解密时,它不会回到原来的状态。这是代码,
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()
问题是它不显示原始文本。这似乎让它变得更加神秘。这里出了什么问题?请帮忙。
更新: a 改变了它,所以它只增加了 5。并且它有效。所以这告诉我这是我添加最后一个字符代码值的部分。还有一个问题:它添加了一个新行和这个奇怪的行字符(不是管道 ---> |)。
现在代码已修复。谢谢。这是结果: WW1ueCVueCV1d2p5eX4laHR0cTMlWW1mc3AlfnR6JXh5ZmhwJXR7andrcXR8JWt0dyV5bWolZnN4fGp3Mw8=
在我解密之后,所以它不会带走五个,它起作用了。再次感谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能将任意字节放入文本区域。
您的加密算法纯粹是按字节计算的 - 它适用于字符的数值。即使对于 ASCII,这也是行不通的,因为字节 0 是 ASCII
NUL
- 经过特殊处理,而字节 127(“Z”,字符 122,向上移动 5 后变成的内容)是DEL
字符:不是您想要的!除了 ASCII 之外,现实世界的 UTF-8 还具有数千个无效字节序列。如果您要使用这样的字节加密,则无法将结果放入文本字段 - 它是二进制数据。如果您希望它能够进入文本字段,则必须确保它是有效文本,不含控制字符、
NUL
字节或无效字节序列。保持二进制卫生的一个简单方法是在加密时对其进行 Base64 编码,并让解密的第一步是 Base64 解码。
最后注意:您还对文本区域末尾的换行符进行加密。可能不是您想做的事情。
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 aDEL
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.
这似乎是一个相当容易破解的“加密”。每个拥有来源的人都可以阅读您的消息。安全的密码系统不应依赖于算法的安全。
您可以使用 Python 加密工具包 (www.dlitz.net/software/pycrypto/)。它有许多不同的可用加密算法。
您可以在以下位置找到有关如何使用它的一些示例: http:// www.laurentluce.com/posts/python-and-cryptography-with-pycrypto/
如果您不关心安全性,为什么不使用例如标准编码之一(请参阅标准的§7.8“编解码器”)图书馆 参考);
凯撒密码:
Base64 编码:
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:
Base64 encoding: