简体   繁体   English

如何在 Mac 上将 python 程序 append 制作成受密码保护的文件

[英]How to make a python program append to a password protected file on Mac

I'm writing a secure password generator python program.我正在编写一个安全密码生成器 python 程序。 I've written the code and I am able to write the desired information to the file as well, but I would like the file to be password protected(Owned by the Admin).我已经编写了代码,我也可以将所需的信息写入文件,但我希望该文件受密码保护(由管理员拥有)。 And the program shows an error because it cannot access the file:并且程序显示错误,因为它无法访问该文件:

file = open("passwords.txt", "a")
PermissionError: [Errno 1] Operation not permitted: 'passwords.txt'

How can I make it so that every time I run the program, it prompts the user to enter the file's password and then appends to it?我怎样才能让它每次运行程序时,它都会提示用户输入文件的密码,然后附加到它?

Here is my code so far:到目前为止,这是我的代码:

import stdio
import stdrandom


def randChar(a):
    stdrandom.shuffle(a)
    return a[0]


def randLetter(a):
    stdrandom.shuffle(a)
    return a[0]


def u_randLetter(a):
    stdrandom.shuffle(a)
    y = a[0]
    return y.upper()

def rand_num():
    return stdrandom.uniformInt(0, 10)


def main():
    special_Char = ["!", "@", "#", "$", "%", "^", "&", "*", "-", "+"]
    alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
                "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

    password = []
    for i in range(4):
        password.append(randLetter(alphabet))
        password.append(rand_num())
        password.append(u_randLetter(alphabet))
        password.append(randChar(special_Char))

    stdrandom.shuffle(password)

    for j in range(len(password)):
        stdio.write(password[j])

    stdio.writeln()

    stdio.writeln("Username:")
    username = stdio.readString()
    stdio.writeln("Website:")
    website = stdio.readString()
    file = open("passwords.txt", "a")
    file.write(username + "\n")
    file.write(website + "\n")
    file.write("    Password: ")

    for j in range(len(password)):
        file.write(str(password[j]))
    file.write("\n")
    file.write("\n")
    file.write("\n")


if __name__ == '__main__':
    main()

I think there are two options to solve this issue for you;我认为有两种选择可以为您解决这个问题; if you want to add a password to your file on your mac, there is a [user guid][1].如果您想为 Mac 上的文件添加密码,可以使用 [user guid][1]。 There is also an option to do the same in a folder and hide it if you want.如果需要,还可以选择在文件夹中执行相同操作并隐藏它。 If you want to be able to see the file still but the content will be unreadable.如果您希望仍能看到文件,但内容将不可读。 You can encrypt the file [example on how to do that][2].您可以加密文件 [关于如何做到这一点的示例][2]。 Note: you will have to save the key to decrypt the file content;注意:您必须保存密钥才能解密文件内容; you can use the password in the following way.您可以通过以下方式使用密码。

  1. chose a password (1 to 8 is excellent;)选择密码(1 到 8 最好;)
  2. using this password to encrypt the data.使用此密码加密数据。
  3. hash the password [example on how to hash][3] and save it. hash 密码 [关于如何散列的示例][3] 并保存。
  4. in the python script, check if the password after hashing it == to the saved hashed password.在 python 脚本中,检查散列后的密码是否 == 保存的散列密码。
  5. if they are the same, use the password to decrypt the file content.如果相同,则使用密码解密文件内容。

[1]: https://support.apple.com/en-il/guide/pages/tanca246d3ac/mac#:~:text=Choose%20File%20%3E%20Set%20Password%20(from,password%20in%20my%20keychain%E2%80%9D%20appears. [2]: https://www.geeksforgeeks.org/encrypt-and-decrypt-files-using-python/ [3]: https://www.programiz.com/python-programming/methods/built-in/hash [1]: https://support.apple.com/en-il/guide/pages/tanca246d3ac/mac#:~:text=Choose%20File%20%3E%20Set%20Password%20(from,password%20in %20my%20keychain%E2%80%9D%20appears. [2]: https://www.geeksforgeeks.org/encrypt-and-decrypt-files-using-python/ [3]: https://www.programiz .com/python-programming/methods/built-in/hash

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

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