使用密码加密和解密文件

时间:2019-03-27 18:06:56

标签: linux file encryption password-encryption

我正在使用linux,基本上我想使用密码对文件进行加密。

我曾尝试使用gpg -c myfile进行加密,但是效果很好,它要求我输入密码并对其进行加密。但是它仅在加密时要求输入密码。

我想要一种加密文件的方法,如果要解密它,则必须提供与加密文件相同的密码。


如果有一个python库也可以工作,因为我可以将其放在脚本上。

2 个答案:

答案 0 :(得分:0)

有几种方法可以在Linux下创建受密码保护的文件。

GnuPG

GnuPG可用于加密数据和创建数字签名。

要加密和解密data.txt文件,请使用gpg命令,如下所示:

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

mcrypt

mcrypt允许您类似于GnuPG创建受密码保护的文件

要加密和解密data.txt文件,请使用mcrypt命令,如下所示:

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

OpenSSL

OpenSSl密码学工具包还可用于加密和解密文件和消息。

要加密和解密data.txt文件,请按以下方式使用openssl命令:

$ 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

答案 1 :(得分:0)

这是因为gpg-agent是一个管理私钥的守护进程,该守护进程用作gpg的后端。默认情况下,它将密码短语缓存一段时间。您可以使用以下选项(来自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).

清除缓存的一种方法是重新加载gpg-agent:gpgconf --reload gpg-agent

您可以使用gpg -c myfile && gpgconf --reload gpg-agent来加密文件,然后将询问您是否尝试使用gpg myfile.gpg解密文件的密码

相关问题