为了账号安全,请及时绑定邮箱和手机立即绑定

ReferenceError:openpgp 未定义

ReferenceError:openpgp 未定义

慕婉清6462132 2023-11-02 22:40:41
我正在创建一个简单的 chrome 扩展,可以使用 openPGP 的库加密和解密字符串。但是,我似乎无法定义 openGPG 并且不断收到错误:ReferenceError: openpgp is not defined我已经在 HTML 文件中定义了 openGPG 库,我认为它可以在全球范围内使用,对吗?我的 HTML 和 JS 代码如下用我的加密的JS文件。非常早期的版本,但只是想在定义键并添加该逻辑之前运行它。    document.getElementById("encryptTest").addEventListener('click', () => {        console.log("Popup DOM fully loaded and parsed");            async function encryptString() {            // put keys in backtick (``) to avoid errors caused by spaces or tabs            const publicKeyArmored = `-----BEGIN PGP PUBLIC KEY BLOCK-----        ...        -----END PGP PUBLIC KEY BLOCK-----`;            const privateKeyArmored = `-----BEGIN PGP PRIVATE KEY BLOCK-----        ...        -----END PGP PRIVATE KEY BLOCK-----`; // encrypted private key            const passphrase = `yourPassphrase`; // what the private key is encrypted with                    const { keys: [privateKey] } = await openpgp.key.readArmored(privateKeyArmored);            await privateKey.decrypt(passphrase);                    const { data: encrypted } = await openpgp.encrypt({                message: openpgp.message.fromText('Hello, World!'),                 // input as Message object                publicKeys: (await openpgp.key.readArmored(publicKeyArmored)).keys, // for encryption                privateKeys: [privateKey]                                           // for signing (optional)            });            console.log(encrypted); // '-----BEGIN PGP MESSAGE ... END PGP MESSAGE-----'            const { data: decrypted } = await openpgp.decrypt({                message: await openpgp.message.readArmored(encrypted),              // parse armored message                publicKeys: (await openpgp.key.readArmored(publicKeyArmored)).keys, // for verification (optional)                privateKeys: [privateKey]                                           // for decryption            });            console.log(decrypted); // 'Hello, World!'        });    });
查看完整描述

1 回答

?
千巷猫影

TA贡献1829条经验 获得超7个赞

我认为问题在于该函数在您的代码中是孤立的。还要确保在 HTML 文件中正确定义它。欢迎来到堆栈溢出:)!



查看完整回答
反对 回复 2023-11-02
  • 1 回答
  • 0 关注
  • 56 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信