【问题标题】:gpg not asking for a passphrase in a bash scriptgpg 不要求在 bash 脚本中输入密码
【发布时间】:2021-04-16 23:52:56
【问题描述】:

我试图编写一个 bash 脚本来解密 gpg 文件并输出结果(这样我就可以将密码复制到应用程序中)。然后它会擦除文件。

在 bash 脚本之外成功提示输入密码。

但是,当我运行 bash 脚本时,它会直接跳转到输出文件内容而不提示输入密码?

脚本如下。

我刚开始编写脚本 - 谁能告诉我哪里出错了?

谢谢

1 ###################################
2 # securely decrypt password file and delete
3 ###################################
4 #secure wipe gpgout directory
5 wipe -rfi ~/programs/utils/scripts/gpg/gpgout/*
6 #decrypt p1.gpg to file p1.txt
7 gpg -o ~/programs/utils/scripts/gpg/gpgout/p1.txt -d ~/programs/utils/scripts/gpg/p1.gpg
8 #clear cached password
9 echo RELOADAGENT | gpg-connect-agent
10 #print p1.txt password to the terminal
11 cat p1.txt
12 #secure wipe gpgout directory again to remove p1.txt
13 wipe -rfi ~/programs/utils/scripts/gpg/gpgout/*
14 #silence bash output
15 #https://www.baeldung.com/linux/silencing-bash-output
16 command > /dev/null 2> /dev/null
                              

【问题讨论】:

  • 可以肯定的是,您是在问为什么您的 bash 脚本不查询密码并且仍然毫无问题地解密 p1.gpg 文件?
  • 完全正确 - 我希望脚本提示输入密码 - 不确定我是否需要明确告诉它在 bash 脚本中执行此操作? gpg -o ~/programs/utils/scripts/gpg/gpgout/p1.txt -d ~/programs/utils/scripts/gpg/p1.gpg
  • 在脚本之外工作正常并提示输入密码。在脚本里面它没有,只是给了我解密的结果?谢谢

标签: linux bash gnupg


【解决方案1】:

您似乎正在使用存储密钥和密码的 gpg-agent。 为了防止 gpg-agent 向 gpg 提供密钥/密码,您应该在 gpg 调用之前强制 gpg-agent 重新加载:

###################################
# securely decrypt password file and delete
###################################
#secure wipe gpgout directory
wipe -rfi ~/programs/utils/scripts/gpg/gpgout/*
#decrypt p1.gpg to file p1.txt
#clear cached password
echo RELOADAGENT | gpg-connect-agent
gpg -o ~/programs/utils/scripts/gpg/gpgout/p1.txt -d ~/programs/utils/scripts/gpg/p1.gpg
#clear cached password
echo RELOADAGENT | gpg-connect-agent
#print p1.txt password to the terminal
cat p1.txt
#secure wipe gpgout directory again to remove p1.txt
wipe -rfi ~/programs/utils/scripts/gpg/gpgout/*
#silence bash output
#https://www.baeldung.com/linux/silencing-bash-output
command > /dev/null 2> /dev/null

我还会建议您使用更好的方法(无子shell)来处理 gpg-connect-agent :

gpg-connect-agent reloadagent /bye

而不是

echo RELOADAGENT | gpg-connect-agent

【讨论】:

    猜你喜欢
    • 2011-02-08
    • 1970-01-01
    • 1970-01-01
    • 2012-05-17
    • 1970-01-01
    • 2018-07-07
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    相关资源
    最近更新 更多