人手一个工具站:前端 PGP 加解密 (单 html+单 js) - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
请不要在回答技术问题时复制粘贴 AI 生成的内容
busier

人手一个工具站:前端 PGP 加解密 (单 html+单 js)

  •  
  •   busier Jun 17, 2024 892 views
    This topic created in 679 days ago, the information mentioned may be changed or developed.
    在纯前端实现的 PGP 加解密( firefox 115.12 测试通过)

    代码中引用的 openpgp.min.js 是 openpgpjs 在这里发布的项目: https://unpkg.com/openpgp/dist/

    直接下载此 js 文件: https://unpkg.com/[email protected]/dist/openpgp.min.js

    将以下代码保存成 .html 文件即可

    <!DOCTYPE html>
    <html lang="en">

    <head>
    <meta charset="UTF-8">
    <meta name="viewport" cOntent="width=device-width, initial-scale=1.0">
    <title>OpenPGP.js Demo</title>
    <script src="openpgp.min.js"></script>
    <script>
    function clearTextarea() {

    switch (event.target.id) {
    case "btnClearPubKey2":
    var text = document.getElementById("publicKey2").value = "";
    break;
    case "btnClearPgpMsg2":
    var text = document.getElementById("pgpmsg2").value = "";
    break;
    }
    }

    function copyToClipboard() {

    switch (event.target.id) {
    case "btnCopyPubKey":
    var text = document.getElementById("publicKey").value;
    break;
    case "btnCopyPgpMsg":
    var text = document.getElementById("encrypttext").value;
    break;
    }

    // Create a temporary textarea element
    var tempTextarea = document.createElement("textarea");
    tempTextarea.value = text;
    document.body.appendChild(tempTextarea);

    // Select the text in the textarea element
    tempTextarea.select();
    tempTextarea.setSelectionRange(0, 99999); /*For mobile devices*/

    // Copy the selected text
    document.execCommand("copy");

    // Remove the temporary textarea element
    document.body.removeChild(tempTextarea);

    // Optionally, provide feedback to the user
    //alert("Copied the text: " + text);
    }

    async function pgpencrypt() {

    // privateKey sign

    var privateKeyArmored = document.getElementById("privateKey").value;

    const passphrase = document.getElementById("passphrase").value;

    const privateKey = await openpgp.decryptKey({
    privateKey: await openpgp.readPrivateKey({ armoredKey: privateKeyArmored }),
    passphrase
    });

    // The other party's public key is used for encryption

    var publicKeyArmored2 = document.getElementById("publicKey2").value;

    var plaintext = document.getElementById("plaintext").value;

    const publicKey = await openpgp.readKey({ armoredKey: publicKeyArmored2 });

    const encrypted = await openpgp.encrypt({
    message: await openpgp.createMessage({ text: plaintext }), // input as Message object
    encryptionKeys: publicKey,
    signingKeys: privateKey // optional
    });

    document.getElementById('encrypttext').value = encrypted;
    //console.log(encrypted); // '-----BEGIN PGP MESSAGE ... END PGP MESSAGE-----'

    }

    async function pgpdecrypt() {

    // Verify digital signature with the opponent's public key

    var publicKeyArmored2 = document.getElementById("publicKey2").value;

    const publicKey2 = await openpgp.readKey({ armoredKey: publicKeyArmored2 });

    // decryption

    var privateKeyArmored = document.getElementById("privateKey").value;

    var encrypted = document.getElementById("pgpmsg2").value;

    const passphrase = document.getElementById("passphrase").value; // what the private key is encrypted with

    const privateKey = await openpgp.decryptKey({
    privateKey: await openpgp.readPrivateKey({ armoredKey: privateKeyArmored }),
    passphrase
    });

    const message = await openpgp.readMessage({
    armoredMessage: encrypted // parse armored message
    });

    const { data: decrypted, signatures } = await openpgp.decrypt({
    message,
    verificationKeys: publicKey2, // optional
    decryptionKeys: privateKey
    });

    document.getElementById('decryptedtext').value = decrypted
    //console.log(decrypted); // 'Hello, World!'
    // check signature validity (signed messages only)

    try {
    await signatures[0].verified; // throws on invalid signature
    console.log('Signature is valid');
    } catch (e) {
    throw new Error('Signature could not be verified: ' + e.message);
    }

    }

    async function buildpair() {
    const { privateKey, publicKey, revocationCertificate } = await openpgp.generateKey({
    type: 'ecc', // Type of the key, defaults to ECC
    curve: 'curve25519', // ECC curve name, defaults to curve25519
    userIDs: [{ name: 'Jon Smith', email: '[email protected]' }], // you can pass multiple user IDs
    passphrase: document.getElementById("passphrase").value, // rotects the private key
    format: 'armored' // output key format, defaults to 'armored' (other options: 'binary' or 'object')
    });

    document.getElementById('privateKey').value = privateKey; // '-----BEGIN PGP PRIVATE KEY BLOCK ... '
    document.getElementById('publicKey').value = publicKey; // '-----BEGIN PGP PUBLIC KEY BLOCK ... '
    //console.log(revocationCertificate); // '-----BEGIN PGP PUBLIC KEY BLOCK ... '
    }

    (async () => {
    const { privateKey, publicKey, revocationCertificate } = await openpgp.generateKey({
    type: 'ecc', // Type of the key, defaults to ECC
    curve: 'curve25519', // ECC curve name, defaults to curve25519
    userIDs: [{ name: 'Jon Smith', email: '[email protected]' }], // you can pass multiple user IDs
    passphrase: 'password', // protects the private key
    format: 'armored' // output key format, defaults to 'armored' (other options: 'binary' or 'object')
    });

    document.getElementById('privateKey').value = privateKey; // '-----BEGIN PGP PRIVATE KEY BLOCK ... '
    document.getElementById('publicKey').value = publicKey; // '-----BEGIN PGP PUBLIC KEY BLOCK ... '
    //console.log(revocationCertificate); // '-----BEGIN PGP PUBLIC KEY BLOCK ... '
    })();
    </script>
    </head>

    <body>
    <h1>OpenPGP.js Demo</h1>
    <hr />
    <h3>My temporary PGP key pair</h3>
    <p>
    Private key protects passwords<br />
    <input id="passphrase" type="password" value="password" />
    <button id="btnBuildPair">Build pair</button>
    </p>
    <p><textarea id="privateKey" rows="5" cols="70"></textarea></p>
    <p><textarea id="publicKey" rows="5" cols="70"></textarea></p>
    <p><button id="btnCopyPubKey">Copy Public Key</button></p>
    <hr />
    <h3>Encrypt Message</h3>
    <p>The PGP public key of the other party</p>
    <p><button id="btnClearPubKey2">Clear</button></p>
    <p><textarea id="publicKey2" rows="5" cols="70"></textarea> </p>
    <p>Plain Text</p>
    <p><textarea id="plaintext" rows="5" cols="70"></textarea></p>
    <p><button id="btnPgpSignEncrypt">Signature & Encrypt</button></p>
    <p><textarea id="encrypttext" rows="5" cols="70"></textarea></p>
    <p><button id="btnCopyPgpMsg">Copy PGP Message</button></p>
    <hr />
    <h3>Decrypt Message</h3>
    <p>PGP Message</p>
    <p><button id="btnClearPgpMsg2">Clear</button></p>
    <p><textarea id="pgpmsg2" rows="5" cols="70"></textarea></p>
    <p><button id="btnPgpDecryptVerifySign">Decrypt & Verify Signature</button></p>
    <p>Plain Text</p>
    <p><textarea id="decryptedtext" rows="5" cols="70"></textarea></p>
    <script>

    document.getElementById("btnBuildPair").addEventListener("click", buildpair);

    document.getElementById("btnPgpSignEncrypt").addEventListener("click", pgpencrypt);

    document.getElementById("btnPgpDecryptVerifySign").addEventListener("click", pgpdecrypt);

    document.getElementById("btnCopyPubKey").addEventListener("click", copyToClipboard);

    document.getElementById("btnCopyPgpMsg").addEventListener("click", copyToClipboard);

    document.getElementById("btnClearPubKey2").addEventListener("click", clearTextarea);

    document.getElementById("btnClearPgpMsg2").addEventListener("click", clearTextarea);

    </script>
    </body>

    </html>
    No Comments Yet
    About     Help     Advertise     Blog     API     FAQ     Solana     968 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 35ms UTC 19:55 PVG 03:55 LAX 12:55 JFK 15:55
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86