使用批处理文件解密 GPG 文件

use*_*897 5 encryption gnupg batch-file

我正在使用带有以下代码的批处理文件解密 gpg 文件。

gpg.exe --output test.csv --batch --passphrase-fd 0 --decrypt WSB330TJ.CSTDJIDF.TXT.asc.14.04.22_00.59.gpg
Run Code Online (Sandbox Code Playgroud)

虽然它确实解密了文件,但我必须手动输入密码。我如何改进它,以便它自动选择密码并解密文件,而无需任何手动干预?我应该在这里添加什么?

Jen*_*rat 6

您告诉 GnuPG 使用 来从 stdin 读取密码--passphrase-fd 0。有不同的选项可以读取密码,来自man gpg

   --passphrase-fd n
          Read the passphrase from file descriptor n.  Only  the  first  line
          will  be  read  from  file  descriptor  n.  If you use 0 for n, the
          passphrase will be read from STDIN. This can only be used  if  only
          one passphrase is supplied.

   --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 ques-
          tionable security if other users can read this file. Don't use this
          option if you can avoid it.

   --passphrase string
          Use  string  as  the  passphrase. This can only be used if only one
          passphrase is supplied. Obviously, this  is  of  very  questionable
          security  on  a multi-user system. Don't use this option if you can
          avoid it.
Run Code Online (Sandbox Code Playgroud)

如果您使用 GnuPG 2,请记住使用--batch,否则密码选项将被忽略。

如果您将密码存储在文件中,请使用--passphrase-file password.txt,如果您想将其作为字符串传递,请使用--passphrase "f00b4r"(当然,两次都使用适当的参数值)。

@Thierry 在评论中指出(特别是在使用 Windows 时)请确保以 UNIX 换行符 ( \n/LN) 而不是 Windows 换行符 + 回车符 ( \n\r/LNRF) 结束文件。