Putting password in a python file

Asked

Viewed 1,629 times

1

I’m making a program in which you select the file you want to lock using a password by clicking on a button that file is locked with a user-defined password. How can I make this file only be accessed with the password?

1 answer

1

I believe you can use a library like the Pycrypto to encrypt and decrypt your file with a password.

Take a look in this topic and in this also.

Create a function to proteger() and one for desproteger(), which receives both the file path and the access password.

  • proteger('arq.txt', 'senha_dificil123')
  • proteger('arq.txt', 'senha_dificil123')

Just take a look at documentation to understand how to perform these procedures.


Another alternative would be to create a function to compress the file and put a password to unzip the file as shown in this topic.

To create an encrypted file 'meu_arquivo.zip' using the open-source solution 7-Zip:

x = subprocess.call(['7z', 'a', '-pP4$$W0rd', '-y', 'meu_arquivo.zip'] + 
                     ['arquivo1.txt', 'arquivo2.txt'])

To install the 7-Zip:

In the Linux, to install type in the terminal:

$ sudo apt-get install p7zip-full

To manually unzip the created file, type: $ unzip meu_arquivo.zip and type 'P4$$W0rd' at the prompt that appears asking for the password.

Already in the Windows just rotate the installer .exe.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.