|
| 1 | +var generateKeyButton = document.getElementById('generate-key'); |
| 2 | + var encryptButton = document.getElementById('encrypt'); |
| 3 | + var decryptButton = document.getElementById('decrypt'); |
| 4 | + var title = document.getElementById('title'); |
| 5 | + var privateKeyTextArea = document.getElementById('private-key'); |
| 6 | + var publicKeyTextArea = document.getElementById('public-key'); |
| 7 | + var passphraseInput = document.getElementById('passphrase'); |
| 8 | + |
| 9 | + function setActiveButton(button) { |
| 10 | + generateKeyButton.classList.remove('active'); |
| 11 | + encryptButton.classList.remove('active'); |
| 12 | + decryptButton.classList.remove('active'); |
| 13 | + button.classList.add('active'); |
| 14 | + } |
| 15 | + |
| 16 | + function setStatusMessage(message) { |
| 17 | + title.innerText = message; |
| 18 | + } |
| 19 | + |
| 20 | + function handleEncryption() { |
| 21 | + var publicKey = publicKeyTextArea.value; |
| 22 | + var plaintext = document.getElementById('input-text').value; |
| 23 | + var passphrase = passphraseInput.value; |
| 24 | + |
| 25 | + var pgpOptions = { |
| 26 | + message: openpgp.message.fromText(plaintext), |
| 27 | + publicKeys: openpgp.key.readArmored(publicKey).keys |
| 28 | + }; |
| 29 | + |
| 30 | + openpgp.encrypt(pgpOptions).then(function(ciphertext) { |
| 31 | + var encryptedText = ciphertext.data; |
| 32 | + document.getElementById('output-text').value = encryptedText; |
| 33 | + setStatusMessage("Message encrypted successfully."); |
| 34 | + }).catch(function(error) { |
| 35 | + console.log(error); |
| 36 | + setStatusMessage("Error encrypting message."); |
| 37 | + }); |
| 38 | + } |
| 39 | + |
| 40 | + function handleDecryption() { |
| 41 | + var privateKey = privateKeyTextArea.value; |
| 42 | + var passphrase = passphraseInput.value; |
| 43 | + var encryptedText = document.getElementById('output-text').value; |
| 44 | + |
| 45 | + var pgpOptions = { |
| 46 | + message: openpgp.message.readArmored(encryptedText), |
| 47 | + privateKey: openpgp.key.readArmored(privateKey).keys[0], |
| 48 | + passphrase: passphrase |
| 49 | + }; |
| 50 | + |
| 51 | + openpgp.decrypt(pgpOptions).then(function(plaintext) { |
| 52 | + var decryptedText = plaintext.data; |
| 53 | + document.getElementById('input-text').value = decryptedText; |
| 54 | + setStatusMessage("Message decrypted successfully."); |
| 55 | + }).catch(function(error) { |
| 56 | + console.log(error); |
| 57 | + setStatusMessage("Error decrypting message."); |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + generateKeyButton.addEventListener('click', function() { |
| 62 | + setActiveButton(generateKeyButton); |
| 63 | + setStatusMessage("Generating key pair, please wait..."); |
| 64 | + |
| 65 | + var pgpOptions = { |
| 66 | + userIds: [{ name: 'Your Name', email: '[email protected]' }], |
| 67 | + rsaBits: 2048, |
| 68 | + passphrase: passphraseInput.value |
| 69 | + }; |
| 70 | + |
| 71 | + openpgp.generateKey(pgpOptions).then(function(key) { |
| 72 | + var privKey = key.privateKeyArmored; |
| 73 | + var pubKey = key.publicKeyArmored; |
| 74 | + |
| 75 | + privateKeyTextArea.value = privKey; |
| 76 | + publicKeyTextArea.value = pubKey; |
| 77 | + document.getElementById('output-text').value = ""; |
| 78 | + document.getElementById('input-text').value = ""; |
| 79 | + setStatusMessage("Key pair generated successfully."); |
| 80 | + }).catch(function(error) { |
| 81 | + console.log(error); |
| 82 | + setStatusMessage("Error generating key pair."); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + encryptButton.addEventListener('click', function() { |
| 87 | + setActiveButton(encryptButton); |
| 88 | + setStatusMessage("Encrypting message. Please wait..."); |
| 89 | + handleEncryption(); |
| 90 | + }); |
| 91 | + |
| 92 | + decryptButton.addEventListener('click', function() { |
| 93 | + setActiveButton(decryptButton); |
| 94 | + setStatusMessage("Decrypting message. Please wait..."); |
| 95 | + handleDecryption(); |
| 96 | + }); |
0 commit comments