简体   繁体   中英

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

I'm writing a secure password generator python program. 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]. 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]. 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;)
  2. using this password to encrypt the data.
  3. hash the password [example on how to hash][3] and save it.
  4. in the python script, check if the password after hashing it == to the saved hashed password.
  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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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