如何使用 gpg 非交互方式加密文件?

use*_*097 7 encryption gnupg

我想编写一个脚本,使用 gpg 和保存在文件中的密码自动加密文件。

我试过这个:

gpg -c --passphrase-fd 0 file.txt < pass.txt
Run Code Online (Sandbox Code Playgroud)

当我在 Ubuntu 16.04 服务器上运行它时,它会按预期加密文件。当我在 Ubuntu 18.04 桌面上运行它时,它会使用密码管理器模式对话框要求我输入密码。

如何跳过该对话框并进行非交互式加密?

作为解决方法,我使用 openssl 而不是 gpg 执行此操作:

openssl aes-256-cbc -pass file:pass.txt -e -in file.txt -out file.txt.enc
Run Code Online (Sandbox Code Playgroud)

sud*_*dus 6

我在 Lubuntu 18.04 LTS 中进行了测试。

详情请参阅man gpg

   --passphrase-file file
          Read  the passphrase from file file. Only the first line will be
          read from  file  file.  This  can  only  be  used  if  only  one
          passphrase is supplied. Obviously, a passphrase stored in a file
          is of questionable security if other users can read  this  file.
          Don't  use  this  option  if  you  can avoid it.  Note that this
          passphrase is only used if the  option  --batch  has  also  been
          given.  This is different from GnuPG version 1.x.
Run Code Online (Sandbox Code Playgroud)