需要允许用户选择要加密的文件

问题描述 投票:0回答:0

这是我第一次在这里发帖,但如果有任何帮助,我将不胜感激。免责声明,我是新手程序员,正在学习。

对于我的顶点项目,我创建了一个基本的加密/解密程序,目前将其硬编码为常规 .txt 文件。我想扩展它并允许用户选择要加密和解密的文件。

我把代码弄得一团糟,现在已经筋疲力尽了。

如果有帮助,我的项目也可以在 github 上找到! https://github.com/MannyTata/ProjectSoteria

下面是带有硬编码 .txt 文件的代码。

#Commands to encrypt file
with open('myKey.key', 'rb') as mykey:
    key = mykey.read()

def encFile():
    f = Fernet(key)

    with open('topsecret.txt', 'rb') as original_file:
        original = original_file.read()


    encrypted = f.encrypt(original)

    with open('encrypted_topsecret.txt', 'wb') as encrypted_file:
        encrypted_file.write(encrypted)

下面是我试图做的工作......抱歉,这是一团糟。

#Function to look for file to encrypt
def encFileSelect():
    filepath = filedialog.askopenfilename(title="Open a Text File", filetypes=(("text    files","*.txt"), ("all files","*.*")))
    file2enc = open(filepath,'r')

#Functiont to encrypt file
def encFile(file2enc):
    f = Fernet(key)
    #calls the file select function for user to choose file
    windowInstance()
    encFileSelect()

    with open(file2enc, 'rb') as original_file:
        original = original_file.read()


    encrypted = f.encrypt(original)

    with open('encrypted_topsecret.txt', 'wb') as encrypted_file:
        encrypted_file.write(encrypted)

def windowInstance():
    # Create an instance of window
    win=Tk()

    # Set the geometry of the window
    win.geometry("700x300")

    # Create a label
    Label(win, text="Click the button to open a dialog", font='Arial 16 bold').pack(pady=15)

    # Create a button to trigger the dialog
    button = Button(win, text="Open", command=open_file)
    button.pack()


    win.mainloop()
python file tkinter encryption
© www.soinside.com 2019 - 2024. All rights reserved.