当前位置: 首页 > 知识库问答 >
问题:

使用凯撒密码加密现有的txt文件

郭恩
2023-03-14

我正在一个smal家庭项目中工作,我需要加密用户输入的文本。文本文件是在程序def personal_save()的第一部分创建的:但是我想要实现的是,当用户按下主页上的关闭按钮时,def file_Encryption读取生成的txt文件并对其进行加密。我已经找到了一个代码与简单的加密,并试图改变代码,但没有运气,因为我是新的编程世界。

我已经创建了一个函数,当用户按下退出按钮时,文件应该用静态加密密钥加密,然后根窗口应该关闭。

from tkinter import *
from tkinter import ttk

root = Tk()
root.geometry('1030x400')
Label(root, bg="black").place(x=0, y=0)

fname = StringVar(root, value="Your first name")
lastname = StringVar(root, value="Your last name")


def personal_info():
    persinf = Toplevel(root)
    persinf.geometry('800x500')
    persinf.configure(background="light blue")
    ttk.Entry(persinf, textvar=fname).place(x=40, y=110)
    ttk.Entry(persinf, textvar=lastname).place(x=240, y=110)
    Button(persinf, text='Save', width=15, bg='brown', fg='black', command=personal_save).place(x=580, y=450)

def personal_save():
    with open('Personal information.txt', 'a') as f:
        line = f'{fname.get()}, {lastname.get()}\n'
        f.write(line)


def file_ecryption():
    with open('Personal information.txt', 'r') as e:
        encryption_key = 2
        lowerAlpha = "abcdefghijklmnopqrstuvwxyz"
        upperAlpha = lowerAlpha.upper()
        numbers = "0123456789"
        decrypted = lowerAlpha + upperAlpha + numbers
        encrypted = lowerAlpha[encryption_key:] + lowerAlpha[:encryption_key] + \
                    upperAlpha[encryption_key:] + upperAlpha[:encryption_key] + \
                    numbers[encryption_key:] + numbers[:encryption_key]
        translation = str.maketrans(decrypted, encrypted)
        cipherText = e.translate(translation)
        print("\nCoded Message:  {}".format(cipherText))
        print("\nFrom:  {}".format(decrypted))
        print("  To:  {}\n".format(encrypted))
        print("Encryption key:", encryption_key)
        root.destroy()


Button(root, text='Add personal information', width=25, bg='brown', fg='black', command=personal_info).\
    place(x=50, y=200)

Button(root, text='Close window', width=25, bg='brown', fg='black', command=file_ecryption).\
    place(x=200, y=200)

root.mainloop()

cipherText = e.translate(translation)

共有1个答案

汝弘深
2023-03-14

当您以e的身份打开文件,然后调用e.translate时,您是在文件描述符上调用translate而不是在内容上调用translate。首先你需要阅读E的内容。

 类似资料:
  • 我需要编写一个python、hacker和wow这些单词的加密文本,并且使用不包括使用RAW_INPUT的python中的凯撒密码,距离为3。这是我到目前为止,但我不断得到一个错误消息,我不知道如何修复它。

  • 示例:返回。如果给定的整数值小于0或大于26,则应返回空字符串。 我的问题是,作为回报,它只给我真或假。如何解决这个问题:该方法应该返回一个字符串,其中每个字符都是根据指定的整数值移动的。

  • 问题内容: 我通过java进行了凯撒密码,它可以运行,但是在用户输入密钥后不对任何内容进行加密! 这是我的代码 跑: 问题答案: 使用效率不是很高…您可以对值进行整数运算以获得其索引。 我在代码中添加了注释以解释更多信息,但这就是我想出的。 输出应该是这样的

  • 我想知道如何限制加密的ASCII范围从32-126。 这个是用来解密的 我的加密工作得很好(当我引用解密函数时),但我的解密并不像它应该的那样工作。 额外注意:我们只需要在编码时进行右移,给出一个字符串供整个程序加密和解密(“这是C++”) 注意:在添加更多代码描述的过程中

  • 首先,我是编程的新手,所以要温柔。而且,我一直在做这个任务,但没有任何结果。任务是创建一个凯撒密码程序,它加密或解密一个段落最多100个字符。实际上是两个独立的实验室。第一个实验是加密,然后第二个实验是解密。一旦我弄清楚了如何创建加密程序,解密程序应该很简单,因为我可以只进行语义更改来解密而不是加密。无论如何,这是我到目前为止的代码。它通过了他们给我们的5个测试中的4个,但出于某种原因,有一个测试