简体   繁体   English

如何加密并写入文本文件?

[英]How do I encrypt and write to a text file?

I have the code shown below, which is supposed to encrypt whatever is in the (data) parameter, but when I open the text file, it just lists all the data as 'none'.我有下面显示的代码,它应该加密(数据)参数中的任何内容,但是当我打开文本文件时,它只是将所有数据列为“无”。 Why is this?为什么是这样?


print("Welcome to Caprigel's Password Manager v1.0!\n")

username = input("Enter username for website: ")
password = input("Enter password for website: ")
url = input("Enter website url: ")

def encrypt(data):
    listed = list(data)

    char_as_ints = []

    for char in listed:
        char_as_int = ord(char)
        char_as_int_conv = char_as_int + 64
        char_as_ints.append(char_as_int_conv)

    print(char_as_ints)

with open("pmdata.txt", "a") as f:
    enc_user = encrypt(username)
    enc_pass = encrypt(password)
    enc_url = encrypt(url)

    f.write(f'''

                Username: {enc_user}
                Password: {enc_pass}
                Url: {enc_url}


                                            ''')

Your encrypt function doesn't explicitly return anything, meaning it implicitly returns None .您的encrypt function 没有显式返回任何内容,这意味着它隐式返回None Instead of printing char_as_ints , you should return it:而不是打印char_as_ints ,您应该return它:

def encrypt(data):
    listed = list(data)

    char_as_ints = []

    for char in listed:
        char_as_int = ord(char)
        char_as_int_conv = char_as_int + 64
        char_as_ints.append(char_as_int_conv)

    return char_as_ints # Here!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM