【发布时间】:2016-07-07 00:50:39
【问题描述】:
我正在实现一个 XOR 方法,我想将加密的消息写入一个 txt 文件,但是在我这样做的方式中,我得到了奇怪的字符而不是消息。
代码如下:
from itertools import cycle
msg = 'RUNNINGFAST'
key = 'SADSTORY'
cipher = ''.join(chr(ord(c)^ord(k)) for c,k in zip(msg, cycle(key)))
print('%s ^ %s = %s ' % (msg, key, cipher))
msg = ''.join(chr(ord(c)^ord(k)) for c,k in zip(cipher, cycle(key)))
print('%s ^ %s = %s ' % (cipher, key, msg))
with open("XOR - Msg_Cipher.txt", "w",) as text_file:
text_file.write("msg: %s \nCipher: %s" % (msg, cipher))
输出如下所示:
txt 文件如下所示:
如何获取txt文件中的输出?
感谢您的帮助
【问题讨论】:
-
那个是的输出。它主要由不可打印的字符组成。
标签: python ascii writetofile