【问题标题】:GPG with triple pipe and keyfile具有三重管道和密钥文件的 GPG
【发布时间】:2014-03-16 10:24:55
【问题描述】:

我想使用 xz 进行 tar 压缩,然后使用 gpg 使用密钥文件进行对称加密。 我可以使用 for 循环来实现,但更愿意通过管道来实现。

我尝试了以下方法,但由于“不明确的输入重定向”而无法正常工作:

tar cvf /home/user/backupdir | xz -1 | gpg -c --batch --passphrase-fd 0 --yes --symmetric --cipher-algo TWOFISH --digest-algo SHA512 -o backupdir.tar.xz.gpg < keyfile

有人可以帮我解决这个问题吗,通过管道加密和解密?

谢谢!

【问题讨论】:

  • 您的 tar 不支持 --xz 选项?

标签: linux bash shell encryption gnupg


【解决方案1】:

你基本上是在做:

tar | xz | gpg <file

在这个链中,你告诉 gpg 从文件和上一个命令中获取输入。

只有一个 STDIN 是您无法从两个来源读取的。

手册页提供了一些解决方案:

   --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.  Note that this passphrase is only used if
          the option --batch has also been given.  This is different from gpg.

   --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 sup‐
          plied. Obviously, a passphrase stored in a file is  of  questionable
          security if other users can read this file. Don't use this option if
          you can avoid it.  Note that this passphrase is  only  used  if  the
          option --batch has also been given.  This is different from gpg.

   --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.  Note that this passphrase is  only  used  if  the  option
          --batch has also been given.  This is different from gpg.

由于您已经在文件中拥有密码短语,因此第二个选项看起来很适合您。

如果第二个选项在您的 gpg 版本中不可用或不起作用,您可以使用第一个选项:

gpg --passphrase-fd 3 3<keyfile

在这种情况下,gpg 将获取 STDIN 上的数据和文件描述符 3 上的密钥文件。

【讨论】:

  • 请注意,--passphrase-file 选项仅在您同时使用 --batch 时才有效。我错过了,一直想知道为什么它不起作用。
【解决方案2】:

在您的情况下,gpg 从标准输入获取其加密数据,但您还指定密码来自标准输入,--passphrase-fd 0。这会混淆 gpg。

也许cat 密钥文件到另一个文件描述符(比如3),在开头使用cat keyfile &gt; &amp;3,然后使用--passphrase-fd 3

【讨论】:

  • 您可以使用gpg --passphrase-fd 3 3&lt;keyfile,但gpg --passphrase-file keyfile 可能是更简单的方法。
  • 在线手册页没有提到 --passphrase-file 选项,因此是解决方法。
  • Windows7/cygwin/bash4.4.12 这有效:cat data | gpg --passphrase-fd 3 3
【解决方案3】:

对于这些情况,存在命名或匿名管道。您的命令行也有多个设计缺陷。

1) 仔细阅读手册页。 gpg 中的 -c 和 --symmetric 标志也是如此。

2) gpg 使用压缩作为标准,因此您的命令行中有两种压缩方案。删除 xz 或使用 gpg 中的 -z 0 标志。

3) --cipher-algo 和 --digest-algo 标志,没有按照你的想法做,在这里将毫无用处。它们仅用于消息加密。使用 --s2k-* 标志代替对称加密。

例子:

KEY="secret" or KEY="$(cat /path/to/keyfile)"
tar -cvf - /home/user/backupdir | gpg --yes --batch --symmetric --passphrase-file <(echo "$KEY") --s2k-cipher-algo twofish --s2k-digest-algo sha512 -o backupdir.tar.gpg

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-16
    • 2020-10-15
    • 2011-08-01
    • 2018-12-26
    • 1970-01-01
    • 1970-01-01
    • 2022-09-24
    • 2018-11-06
    相关资源
    最近更新 更多