Jorge Rosas
Jorge Rosas

Reputation: 51

Encrypt and decrypt files with password

I'm using linux and I basically want to encrypt a file using a password.

I've tried using gpg -c myfile for encryption, and that works fine, it asks me for a password and encrypts it. But it only asks for a password when encrypting.

I want a way to encrypt a file and if you want to decrypt it you have to give the same password that it was encrypted with.


If there's a python library that would work too since I can put that on a script.

Upvotes: 1

Views: 8401

Answers (2)

Mathieu Rollet
Mathieu Rollet

Reputation: 2364

That's because of gpg-agent, a daemon that manages private keys and which is used as a backend for gpg. It caches your passphrases for some time by default. You can configure that with the following options (from man gpg-agent):

--default-cache-ttl n
              Set the time a cache entry is valid to n seconds.  The default is 600 seconds.  Each time a cache entry is accessed, the entry's timer is reset.  To set an entry's maximum
              lifetime,  use  max-cache-ttl.   Note that a cached passphrase may not evicted immediately from memory if no client requests a cache operation.  This is due to an internal
              housekeeping function which is only run every few seconds.

--max-cache-ttl n
              Set  the  maximum time a cache entry is valid to n seconds.  After this time a cache entry will be expired even if it has been accessed recently or has been set using gpg-
              preset-passphrase.  The default is 2 hours (7200 seconds).

One way to clear the cache is to reload the gpg-agent : gpgconf --reload gpg-agent

You can use gpg -c myfile && gpgconf --reload gpg-agent to encrypt your file, after which the password will be asked if you try to decrypt it with gpg myfile.gpg

Upvotes: 2

Timo Strotmann
Timo Strotmann

Reputation: 391

There are several alternatives to create passowrd protected files under Linux.

GnuPG

GnuPG can be used to encrypt data and create digital signatures.

To encrypt and decrypt a data.txt file, use gpg command as follows:

$ gpg -c data.txt
$ gpg data.txt.gpg

mcrypt

mcrypt allows you to create password protected files similarly to GnuPG

To encrypt and decrypt a data.txt file, use mcrypt command as follows:

$ mcrypt data.txt
$ mcrypt -d data.txt.nc

OpenSSL

The OpenSSl Cryptography Toolkit can also be used to encrypt and decrypt files and messages.

To encrypt and decrypt a data.txt file, use the openssl command as follows:

$ openssl enc -aes-256-cbc -salt -in data.txt -out data.txt.enc
$ openssl enc -aes-256-cbc -d -in data.txt.enc -out data.txt

Upvotes: 4

Related Questions