使用ASCII进行文件解密 [英] File decryption with ASCII

查看:123
本文介绍了使用ASCII进行文件解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够加密文件,但是当我解密文件时,它会进入一个循环,创建一个不加解密就逐秒增加大小的海量文件.

I am able to encrypt the file but when I decrypt it goes into a loop creating a mass file increasing size second by second with no decryption.

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")

推荐答案

仔细查看您编写的代码(您可以通过简单的print语句或pdb调试器找到它):

Look closely at the code you wrote (you could have found this with a simple print statement, or the pdb debugger):

    while x < 32:
        x += 96

    while x > 126:
        x -+ 96
           ^

您的意思是x -= 96,而不是x -+ 96.

x -+ 96表示x - (+96)表示x - 96,但这只是一个表达式而不是赋值,没有LHS,它会丢弃结果,而不是将其赋给LHS上的任何内容.因此,任何大于126的原始x值都不会降低到126以下,并且循环永远不会终止. (使用打印语句检查您的代码).

x -+ 96 means x - (+96) which means x - 96, but that's only an expression not an assignment, there's no LHS, it's throwing away the result, not assigning it to anything on the LHS. So any original x value > 126 never gets reduced below 126, and your loop never terminates. (Use print statements to check your code).

为进行故障排除,您至少应弄清楚要坚持的行,并使用pdb(python -m pdb yourprog.py)运行,然后在挂起时按Ctrl-C,控制台将告诉您该行在哪行. (请参见例如调试仅挂起"的python应用程序以及pdb上的所有其他问题和教程.)和/或一旦缩小范围,请输入一些打印语句print("Got here 1")

Hey for troubleshooting, you should at minimum figure out which line it's sticking on, run with pdb (python -m pdb yourprog.py), then Ctrl-C when it's hung and the console will tell you which line it's on. (See e.g. Debugging a python application that just sort of "hangs" and all the many other questions and tutorials on pdb.) And/or once you narrow things down, throw in a few print statements print("Got here 1")

这篇关于使用ASCII进行文件解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆