首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用Caesar Cipher加密现有的txt文件

使用Caesar Cipher加密现有的txt文件
EN

Stack Overflow用户
提问于 2019-07-22 00:48:18
回答 1查看 370关注 0票数 0

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

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

代码语言:javascript
复制
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)

AttributeError:“_io.TextIOWrapper”对象没有属性“”translate“”

EN

回答 1

Stack Overflow用户

发布于 2019-07-22 00:52:57

当您以e打开文件,然后调用e.translate时,您调用的是文件描述符,而不是内容。首先,您需要读取e的内容。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57135164

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档