Openpgp 多密钥加密

Posted

技术标签:

【中文标题】Openpgp 多密钥加密【英文标题】:Openpgp Encrypt with Multiple Keys 【发布时间】:2020-01-04 17:15:04 【问题描述】:

我正在尝试使用多个密钥加密字符串,但我得到了那个错误; TypeError:错误加密消息:key.getPrimaryUser 不是函数

let pubKey = ['-----BEGIN PGP PUBLIC KEY BLOCK-----\r\nVersion: Keybase OpenPGP v1.0.0\r\nComment: https:\/\/keybase.io\/crypto\r\n\r\nxo0EXWqkRQEEAKiSir3G77kAQAjpsNLjIoqBI7Z\/jR5TOaUxO7Ul0NiQn84BIEIG\r\nD44ZkEMjStg+ExH2EQFkjDzdxT633H\/c1NNCfivmFJvLEHYg\/YliQ9dLg8PAW9jl\r\nxn67xGkspogfURdq8i0GpPh28K8RojpRv3U6m5mS5z9SF8yCQwagcIt9ABEBAAHN\r\nFmFzZGFzZCA8YXNkYXNAYXNkLmNvbT7CrQQTAQoAFwUCXWqkRQIbLwMLCQcDFQoI\r\nAh4BAheAAAoJEK+6HfpyjgvY7H4D\/1HotnF3pVVNqG4cLJsH0ix\/za2+07495rkH\r\n+GoWQC2YEdLzV1L41pR\/JmqmQrXZp0M6txpP5\/q6cBQGgcv+u02TLzSzrO0gfzFf\r\nXktVcdr7+8GS2I\/0cFhpqEcxo8ph+uMSZUllzAM+TmeXLbwg8u8otb\/m9cLCc6vn\r\na9mxf86vzo0EXWqkRQEEAObmlGPvG7uCZuRtoccOtiNItzAxMFYGfQvvShvV17Jo\r\nyGP18mt2HF\/9vmb2QDbkNPxIM1JIT54u0Oh0W4Kc3sfCia1kn6CiG+fdbybtvr4z\r\npqyOEsprPe4klFyMIiI10oxE+qTbJn4JViHx12gZCYpjyJr76qIsMWD1r7uRk9Xh\r\nABEBAAHCwIMEGAEKAA8FAl1qpEUFCQ8JnAACGy4AqAkQr7od+nKOC9idIAQZAQoA\r\nBgUCXWqkRQAKCRCz1jcggxBJRrFlA\/9qt11ba9M6R8wV21AoAcWRydSeY7xgMqHO\r\nI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5Zj0GR9QEVmoycmciOIARXm8dRQoA8yIJVK\r\ny+kFOxQPqTkVvk9859DgrHhJJ4G0J0j3ps6CremvW1eeWzbfnGPwLjOKfOpWjv6b\r\nQrMubb7XRZUhxWyJ2fGd\/FNHsOiIOfSOfIaN+0J0FxMPMK5hvTHMkXlZQYbOjQRd\r\naqSrAQQAwvh2OjBbeLMFU2vbH0HsrdbftNnKhI6ZT\/X7MwQZwpNfGLLcMBRx79DZ\r\nFFdwpgwezOUf9zH+mwzGkLfzcU3MWi3Akro\/ZPMun8is8OaVpcd3kWWA6njX0tpj\r\nE43lEzACvNV+uI5hOKm3htKvMZQusjRI67xwBJjWoL2jlR6HyDUAEQEAAcLAgwQY\r\nAQoADwUCXWqkqwUJDwmcAAIbLgCoCRB+jw5+xGpD2J0gBBkBCgAGBQJdaqSrAAoJ\r\nEEvNtk\/PsdsaANAD\/3QVxC6LzphgFv1O4S4gKH1MNZGkfzWmw7XGBriodxwEq3Fh\r\nxkAKh5TsY9UQsTVrvY+PeDaWIEO26ZOCgsc8mW\/oFEppWfB9RpZOw02fTcYU+PZ8\r\nWs61FUGLdDx8+LwIZqXvBMeLdZRXB0GxhDrTuH\/L\/tjZLQ3Et2rvye4Tw4mbnDgD\r\n\/iyrhKzD1hsTPbX9qE0wbFpHQDZr+ud5i+4xpVEtXfanjDFOk2FKNmmT+yGEm7rU\r\nQesXTgkYYpONdvbHwl800mftzPxQlmmfqjZ7P8IYjJvZG0LAbGjsH\/JdevTDDGBK\r\nIEvOTSu34SI68FufqQTDKiWQWo1mBej98qsSoE7SAhcx\r\n=owPm\r\n-----END PGP PUBLIC KEY BLOCK-----'];
let message = "Hello World";

function encryptData(pubkeys, plainText)
    let result;
    let sync = true;
    const encryptDecryptFunction = async() => 
        console.log('working');
        pubkeys = pubkeys.map(async (key) => 
            return (await openpgp.key.readArmored(key)).keys[0]
        );
        const options = 
            message: openpgp.message.fromText(plainText),       // input as Message object
            publicKeys: pubkeys, // for encryption                          // for signing (optional)
        

        openpgp.encrypt(options).then(ciphertext => 
            result = ciphertext.data // '-----BEGIN PGP MESSAGE ... END PGP MESSAGE-----'   
            sync = false;
        ).catch((err) => 
            console.log(err);
        )
    

    encryptDecryptFunction();
    while(sync)require('deasync').sleep(100);
    return result;



encryptData(pubKey, message)

【问题讨论】:

【参考方案1】:

我遇到了同样的问题,它发生是因为在您加载变量 pubkeys 的部分:

  pubkeys = pubkeys.map(async (key) => 
      return (await openpgp.key.readArmored(key)).keys[0]
    )

需要改成这样的promise:

let publicKeysPromises = pubkeys.map(async (key) => 
      return (await openpgp.key.readArmored(key)).keys[0]
    );

由于它是异步操作,并且在完全解决之前在“options”变量中使用“pubkeys”会导致问题。发生这种情况是因为当使用 'options' openpgp.encrypt(options) 调用函数 'encrypt' 时,openPGP 对名为 getPrimaryUser 的函数进行了内部调用。当使用 readArmored 读取公钥时,它会创建一个 Key 对象,该对象又将“getPrimaryUser”作为异步函数附加到它上面。因此,在调用“加密”函数之前,需要先解决上述承诺。

因此,接下来,您需要将选项更改为以下代码:

 const options = 
    message: openpgp.message.fromText(message),
    // resolve all promises returned before
    publicKeys: await Promise.all(publicKeysPromises),       // for encryption
    privateKeys: [privKeyObj]                         // for signing (optional)
    

我希望这能回答这个问题。

【讨论】:

以上是关于Openpgp 多密钥加密的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 OpenPGP.js 在 Objective-C 中加密/解密 PGP 消息

如何将libsodium私钥转换为OpenPGP兼容的私钥包?

无法加密 OpenPGP.js 中的字符串

golang golang gpg / openpgp加密/解密示例

OpenPGP协议的一个JavaScript实现:OpenPGP.js

Openpgp.js无法解密pgp消息