2011-05-26 136 views
1

我想加密和解密字符串。 现在我已经做到了这一点:gpg加密和解密

[email protected]:~$ echo "hallo" | gpg --symmetric --pgp8 --cipher-algo AES256 > /tmp/1 
Enter passphrase: 
Repeat passphrase: 
[email protected]:~$ 
[email protected]:~$ cat /tmp/1 | gpg --decrypt 
gpg: AES256 encrypted data 
Enter passphrase: 
gpg: encrypted with 1 passphrase 
hallo 
[email protected]:~$ 

它的工作原理就像我希望它的工作。现在,我已经使用密码出文件的尝试过,但没有奏效:

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

这是非常有趣的,他询问密码。如果我写错了,我会收到一条错误消息,但是如果我写了正确的密码短语,我就不会收到我的密码字符串。 我的目标是要达到这样的:

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

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

回答

4

您试图通过相同的文件描述符(0,它是stdin)将测试加密(echo "hallo" |)和密码短语(< /home/mis/testgpg)。这些重定向中只有一个可以成功,这是密码短语。为这两项任务使用不同的文件或文件描述符。

例如,使用文件描述符#3密码短语:

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