linux - gpg加密和解密

标签 linux console console-application gnupg

我正在尝试加密和解密字符串。 现在我已经做到了:

mis@fasan:~$ echo "hallo" | gpg --symmetric --pgp8 --cipher-algo AES256 > /tmp/1
Enter passphrase:
Repeat passphrase:
mis@fasan:~$
mis@fasan:~$ cat /tmp/1 | gpg --decrypt
gpg: AES256 encrypted data
Enter passphrase: 
gpg: encrypted with 1 passphrase
hallo
mis@fasan:~$ 

它就像我想要的那样工作。现在我已经尝试使用文件中的密码短语,但它不起作用:

mis@fasan:~$ echo "hallo" | gpg --symmetric --pgp8 --cipher-algo AES256 --passphrase-fd 0 < /home/mis/testgpg > /tmp/1
Reading passphrase from file descriptor 0    
mis@fasan:~$
mis@fasan:~$ cat /tmp/1 | gpg --decrypt
gpg: AES256 encrypted data
gpg: encrypted with 1 passphrase

非常有趣的是,他要求输入密码。如果我写错了,我会收到一条错误消息,但如果我写了正确的密码,我不会得到我的密码字符串。 我的目标是达到这个:

mis@fasan:~$ echo "hallo" | gpg --symmetric --pgp8 --cipher-algo AES256 --passphrase-fd 0 < /home/mis/testgpg > /tmp/1
Reading passphrase from file descriptor 0    
mis@fasan:~$
mis@fasan:~$ cat /tmp/1 | gpg --decrypt --passphrase-fd 0 < /home/mis/testgpg
Reading passphrase from file descriptor 0    
gpg: decrypt_message failed: eof
mis@fasan:~$

但这也行不通。有谁知道我做错了什么?

最佳答案

您正在尝试通过相同的文件描述符(0,即标准输入)推送加密测试 ( echo "hallo" | ) 和密码短语 ( < /home/mis/testgpg )。这些重定向中只有一种可以成功,那就是密码短语。对这两个任务使用不同的文件或文件描述符。

例如,使用文件描述符 #3 作为密码:

echo "hallo" | gpg --symmetric --pgp8 --cipher-algo AES256 --passphrase-fd 3 3< /home/mis/testgpg > /tmp/1

关于linux - gpg加密和解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6138555/

相关文章:

c - 无法使用 GCC/Mingw32 编译器在 C 中使用 "Alt+Space"发送虚拟 key "SendInput()"?

linux - awk 脚本中的 $0 和 $1(美元符号 0 和 1)是什么?

linux - 如何在领域加入事件目录命令中提供密码作为参数

java - Java 如何将文件从一个驱动器 move 到另一个驱动器

java - 用 Java 重新创建扫描仪

javascript - 自动点击提交按钮 Javascript

linux - 在 .fa header id 之后插入连续数字

python - 从控制台调用函数作为参数

c# - 通过 Cmd 启动 Windows 应用程序与控制台应用程序

c# - 有没有一种方法可以搜索用户从文件中加载的文本中的特定单词并为其着色?