【问题标题】:Unable to decrypt an unarmored pgp file with OpenPGP.js: Session key decryption failed无法使用 OpenPGP.js 解密未装甲的 pgp 文件:会话密钥解密失败
【发布时间】:2021-11-13 14:39:32
【问题描述】:

我有一个已加密的 CSV 文件,现在是一个未加密的 PGP 文件。

我正在尝试使用 OpenPGP.js 5.0.0 和 Node.js 14.17.5 对其进行解密,但一直碰壁。

起初,我根据项目示例尝试了以下代码:

const passphrase = 'pass';
    
const publicKeyArmored = '... public ...';
const publicKey = await openpgp.readKey({ armoredKey: publicKeyArmored });
    
const privateKeyArmored = '... private ...';
const privateKey = await openpgp.decryptKey({
  privateKey: await openpgp.readPrivateKey({ armoredKey: privateKeyArmored }),
  passphrase,
});
    
const binaryMessage = fs.createReadStream('/path/to/file.csv.pgp');
const message = await openpgp.readMessage({
  binaryMessage,
});
    
const { data: decrypted, signatures } = await openpgp.decrypt({
  message,
  verificationKeys: publicKey,
  decryptionKeys: privateKey,
});

但是decrypt 函数抛出:Error: Error decrypting message: Session key decryption failed.

然后我尝试添加代码来手动解密会话密钥:

const sessionKey = await openpgp.decryptSessionKeys({
  decryptionKeys: privateKey,
  message,
});

但是decryptSessionKeys 函数抛出:Error: Error decrypting session keys: Session key decryption failed.

我怀疑文件可能被错误地对称加密,所以我尝试将decrypt的调用修改为:

const { data: decrypted, signatures } = await openpgp.decrypt({
  message,
  passwords: passphrase,
});

但是decrypt 函数抛出:Error: Error decrypting message: No symmetrically encrypted session key packet found.

作为健全性测试,我决定尝试使用 gpg CLI 解密文件。

我已使用以下方法导入了铠装密钥对:

gpg --import key.public.pgp
gpg --allow-secret-key-import --import key.private.pgp

然后使用以下命令解密文件:

gpg --show-session-key /path/to/file.csv.pgp

在提示输入私钥密码后,文件成功解密,输出如下:

gpg: WARNING: no command supplied.  Trying to guess what you mean ...
gpg: encrypted with rsa4096 key, ID <redacted>, created 2021-09-02
      "<redacted> (<redacted>) <redacted>"
gpg: used key is not marked for encryption use.
gpg: session key: '<redacted>'

我做错了什么?为什么使用 OpenPGP.js 解密失败,但使用 GnuPG 成功?

【问题讨论】:

    标签: node.js openpgp openpgp.js session-keys


    【解决方案1】:

    消息似乎是使用主密钥加密的,主密钥被标记为仅签名。

    allowInsecureDecryptionWithSigningKeys解密配置设置为true可以解决这个问题:

    const { data: decrypted } = await openpgp.decrypt({
      message,
      config: {
        allowInsecureDecryptionWithSigningKeys: true,
      },
      verificationKeys: publicKey,
      decryptionKeys: privateKey,
    });
    

    【讨论】:

      猜你喜欢
      • 2021-01-22
      • 2019-07-14
      • 2021-02-20
      • 1970-01-01
      • 2019-06-11
      • 1970-01-01
      • 1970-01-01
      • 2018-03-17
      • 2021-02-09
      相关资源
      最近更新 更多