You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无需Node.js,能否在浏览器中使用OpenPGP.js?求实现示例

Can You Use OpenPGP.js Without Node.js?

Absolutely! That claim is totally accurate — OpenPGP.js is built to work smoothly in browser environments, no Node.js required at all. You just need the library files, a modern browser (like Firefox, Chrome, etc.), and a basic HTML file to get started.

Here's a step-by-step breakdown with a working example:

How to Set It Up

  • First, grab the OpenPGP.js package you mentioned. Inside the zip file, look for the dist folder — it contains pre-built browser-ready scripts like openpgp.min.js (the minified version perfect for production use).
  • Create a simple index.html file, and include the library using a standard <script> tag pointing to the local openpgp.min.js file.

Working Example Code

Here’s a minimal, runnable example that generates a key pair, encrypts a message, then decrypts it — all directly in the browser:

<!DOCTYPE html>
<html>
<head>
    <title>OpenPGP.js Browser Demo</title>
    <!-- Include the local OpenPGP.js script -->
    <script src="./openpgp.min.js"></script>
</head>
<body>
    <button onclick="runDemo()">Run Demo</button>
    <pre id="output"></pre>

    <script>
        async function runDemo() {
            const output = document.getElementById('output');
            output.textContent = 'Generating key pair...';

            // Generate an RSA key pair
            const { privateKey, publicKey } = await openpgp.generateKey({
                type: 'rsa',
                rsaBits: 2048,
                userIDs: [{ name: 'Test User', email: 'test@example.com' }],
                passphrase: 'your-strong-passphrase' // Optional but highly recommended for private key protection
            });

            output.textContent += '\nKey pair generated!\nEncrypting message...';

            // Encrypt a message with the public key
            const encrypted = await openpgp.encrypt({
                message: await openpgp.createMessage({ text: 'Hello, OpenPGP.js in browser!' }),
                encryptionKeys: publicKey
            });

            output.textContent += '\nEncrypted message:\n' + encrypted + '\n\nDecrypting message...';

            // Decrypt the message with the private key
            const decrypted = await openpgp.decrypt({
                message: await openpgp.readMessage({ armoredMessage: encrypted }),
                decryptionKeys: await openpgp.decryptKey({
                    privateKey: await openpgp.readPrivateKey({ armoredKey: privateKey }),
                    passphrase: 'your-strong-passphrase'
                })
            });

            output.textContent += '\nDecrypted message:\n' + decrypted.data;
        }
    </script>
</body>
</html>

Quick Notes

  • Ensure the openpgp.min.js file is in the same folder as your index.html, or adjust the src path to match where you stored the library.
  • The example uses async/await, which works in all modern browsers. If you need to support older browsers, you can rewrite it using promise chains with .then().
  • Always treat your private key like sensitive data — if you use a passphrase, make sure it’s strong and only shared with trusted parties.

内容的提问来源于stack exchange,提问作者Anware

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:32:46