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