我能够对文件进行加密,但是当我解密它时会进入一个循环,创建一个大规模文件,逐渐增加大小而不进行解密。
if choice == 3:
string_input = input("Please enter name of file to encrypt: ")
input_offset = int(input("Please enter offset value (1 to 96): "))
encrypted = ""
orig_file = open('C:\\Users\\message.txt', 'r')
encrypted_file = open('new_msg.txt','w')
file_read = orig_file.read()
for file in file_read:
x = ord(file)
encrypted += chr(x + input_offset)
encrypted_file.write(str(x) + " ")
encrypted_file.write(str(encrypted))
while x < 32:
x += 96
while x > 126:
x -+ 96
orig_file.close()
encrypted_file.close()
print("Encrypt successful. Encrypted text written to file: new_msg.txt")
if choice == 4:
string_input = input("Please enter name of file to decrypt: ")
input_offset = int(input("Please enter offset value (1 to 96): "))
decrypted = ""
enc_open_file = open('new_msg.txt', 'r')
decrypted_file = open('orig_msg.txt','w')
enc_file_read = enc_open_file.read()
for file in enc_file_read:
x =ord(file)
decrypted += chr(x - input_offset)
decrypted_file.write(str(x) + " ")
decrypted_file.write(str(decrypted))
while x < 32:
x += 96
while x > 126:
x -+ 96
enc_open_file.close()
decrypted_file.close()
print("Decrypt successful. Decrypted text written to file: orig_msg.txt")
答案 0 :(得分:2)
仔细查看您编写的代码(您可以通过简单的print语句或pdb调试器找到它):
while x < 32:
x += 96
while x > 126:
x -+ 96
^
您的意思是x -= 96
,而不是x -+ 96
。
x -+ 96
表示x - (+96)
,表示x - 96
,但这只是表达而不是作业,没有LHS,它会丢掉结果,而不是将其分配给LHS上的任何内容。那么任何原始x值&gt; 126永远不会减少到126以下,你的循环永远不会终止。 (使用print语句检查代码)。
嘿,为了排除故障,你应该至少弄清楚它坚持哪一行,用pdb(python -m pdb yourprog.py
)运行,然后在它挂起时按Ctrl-C并且控制台会告诉它它是哪一行。 (参见例如Debugging a python application that just sort of “hangs”以及关于pdb的所有其他问题和教程。)和/或一旦缩小范围,请输入一些打印语句print("Got here 1")