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

5
我正在使用以下代码通过批处理文件解密gpg文件。
gpg.exe --output test.csv --batch --passphrase-fd 0 --decrypt WSB330TJ.CSTDJIDF.TXT.asc.14.04.22_00.59.gpg

虽然它确实可以解密文件,但我必须手动输入密码。 如何改进它,使其自动选择密码并在没有任何人工干预的情况下解密文件? 我应该在这里添加什么?


你尝试过gpg-agent吗? - Oleg Estekhin
不,会更简单吗? - user3219897
3个回答

6

使用--passphrase-fd 0命令,您告诉GnuPG从标准输入读取密码。根据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.

如果您使用的是GnuPG 2,请记得使用--batch,否则口令选项将被忽略。
如果您将口令存储在文件中,请使用--passphrase-file password.txt,如果要将其作为字符串传递,请使用--passphrase "f00b4r"(当然,两次都要使用适当的参数值)。
@Thierry在评论中指出(特别是在使用Windows时),请确保以UNIX换行符(\n / LN)结束文件,而不是Windows换行符+回车符(\n\r / LNRF)。

似乎无法使用文件或直接传递密码短语。 - user3219897
gpg.exe --output test.csv --batch --passphrase xyz --decrypt WSB330TJ.CSTDJIDF.TXT.asc.14.04.22_00.59.gpg - user3219897
Jens,还是不行,请查看我描述的回复。 - user3219897
你能解决这个问题吗?我也遇到了类似的问题,有任何更新都会非常有帮助。 - Harika Mamidi
1
我发现在使用 gpg4win 的 --passphrase-file 时,密码文件必须具有 Unix \n 行分隔符而不是 Dos/Windows \r\n 分隔符。 - Thierry
显示剩余3条评论

3

我需要执行gpg --batch --passphrase "MyPassword" --decrypt-files C:\PGPFiles\\\*.pgp*.,才能解密文件。如果在--decrypt-files之前没有使用--passphrase选项,它总是会提示输入密码。同时,当我使用密码文件时,正如Thierry所说,我需要使用Notepad++将其转换为unix风格(编辑->EOL->Unix/OSX 格式)


2

试试这个:

gpg2 -se --passphrase yourpassword --batch --yes -r user@mydomain.com filename

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接