首页 > 解决方案 > Openpgpjs 不会读取密钥

问题描述

我正在尝试在反应应用程序中使用 OpenPGPJS 加密文本。

我在反应类中有一个功能:

const openpgp = require('openpgp');
...
class XYZ extends React.Component{
...
    async encrypt(text, targetKey){
        await openpgp.initWorker({ path: 'openpgp.worker.js' });
        var publicKey = (await openpgp.key.readArmored(targetKey.trim())).keys; 

        console.log(publicKey);
        
        const result = await openpgp.encrypt({
            message: openpgp.message.fromText(text),
            publicKeys: publicKey
        });
        console.log(result.data);
    }
...
const public_key = `-----BEGIN PGP PUBLIC KEY BLOCK----- .......`
const text = 'encrypt this'
this.encrypt(text, public_key);

用于加密。

我收到一条错误消息:Unhandled Rejection (Error): Error encrypting message: No keys, passwords, or session key provided.

我发现(await openpgp.key.readArmored(targetKey.trim())).keys;返回空数组,我知道为什么。

标签: javascriptreactjsopenpgpopenpgp.js

解决方案


推荐阅读