首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ReferenceError:未定义openpgp

ReferenceError:未定义openpgp
EN

Stack Overflow用户
提问于 2020-12-11 00:29:23
回答 1查看 235关注 0票数 0

我正在创建一个简单的chrome扩展,它可以使用openPGP的库对字符串进行加密和解密。然而,我似乎不能定义openGPG并不断得到错误:

代码语言:javascript
复制
ReferenceError: openpgp is not defined

我已经在我的openGPG文件中定义了HTML库,我认为它可以在全球范围内使用,对吗?

我的HTML和JS代码如下

使用我的加密的JS文件。非常早的版本,但只是想在定义键和添加逻辑之前运行它。

代码语言:javascript
复制
    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!'
        }
    
        //We have permission to access the activeTab, so we can call chrome.tabs.executeScript:
        chrome.tabs.executeScript({
            code: '(' + encryptString + ')();' //argument here is a string but function.toString() returns function's code
        });
    });

HTML文件

代码语言:javascript
复制
<!DOCTYPE html>
<html>
  <head>
    <style>
        body {
            width: 350px;
            height: 500px;
        }
      button {
        height: 50px;
        width: 150px;
        outline: none;
      }
      input {
        width: 95%;
        padding: 5px;
        margin-right: 15px;
        margin-top: 50px;
      }
      .decrypt {
        margin-top: 10px;
        width: 150px;
        height: 35px;
      }
    </style>

  </head>
  <body>
      <h1>AIS PGP Exam Encryption</h1>
    <button id="generateKey">Generate Key</button>
    <button id="htmlGrabber">Get HTML</button>
    <input placeholder="Private Key"></input>
    <input placeholder="Message to Decrypt"></input>
    <button class="decrypt">Decrypt Message</button>
    <button id="encryptTest">Test</button>
  </body>
  <script src="../js/jquery-3.5.1.min.js"></script>
  <script src="../js/openpgp.min.js"></script>
  <script src="../js/encrypt.js"></script>
  <script src="../js/htmlGrabber.js"></script>
</html>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-12 00:58:18

我认为问题在于你的代码中的函数是孤立的。还要确保在您的HTML文件中正确定义它。欢迎使用Stack Overflow :)!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65238492

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档