使用命令行对文件夹进行GPG加密和解密

GPG encryption and decryption of a folder using command line

gpg 命令行 (Gnupg) 的手册页有加密和解密文件的命令。这是使用 gpg 的 encrypt/decrypt 文件的标准命令。

gpg --encrypt --recipient xxx@mail.com ~/xxx/xxx.txt - 加密

gpg --output ~/xxx/xxx.txt --decrypt ~/xxx/xxx.gpg - 解密

但是如果我有一个包含多个文件和文件夹的文件夹,我该如何使用命令行对其进行加密?

解决方案一:

使用 gpg-zip。

将用户 Bob 的目录 mydocs 的内容加密到文件 test1:

gpg-zip --encrypt --output test1 --gpg-args  -r Bob mydocs

列出存档test1的内容:

gpg-zip --list-archive test1

这是直接来自 Encrypt or sign files into an archive 的示例。如果您详细阅读该页面,它将对您有很大帮助。

方案二:

将目录变成文件

如果你想加密一个目录,你需要先把它转换成一个文件。 运行 命令:

tar czf myfiles.tar.gz mydirectory/

这会为您提供一个新文件 'myfiles.tar.gz',然后您可以 encrypt/decrypt。将 tarball 转换回目录:

tar xzf myfiles.tar.gz

现在您可以像上面那样使用加密了。所以:

gpg --encrypt --recipient xxx@mail.com ~/xxx/xxx.txt

这直接取自 berkeley encrypting 上的示例,这也是一本快速有用的读物​​。

您可以在此处查看手册页:gnu gpg man

如果您不想将所有内容打包在一起并希望单独加密多个文件:

cd进入文件夹

加密: $ls | gpg --multifile --encrypt$ls | gpg --encrypt-files -r <recipient>

解密: $ls | gpg --multifile --decrypt$ls | gpg --decrypt-files

gpgtar 也是另一种选择。 gpgtar 将文件加密或签名到存档中。它是 gpg 化的 tar,使用与 PGP 的 PGP Zip 相同的格式。

它在 MacOS 和 Linux 上与 gnupg 一起安装。

加密目录

gpgtar --encrypt --output <out_file_name> -r <recipient> <dir_name>

解密目录

gpgtar --decrypt <out_file_name>

gpgtar man page