-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscript.js
More file actions
87 lines (73 loc) · 2.92 KB
/
script.js
File metadata and controls
87 lines (73 loc) · 2.92 KB
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
function showSection(sectionId) {
document.getElementById('generate').style.display = 'none';
document.getElementById('encrypt').style.display = 'none';
document.getElementById('decrypt').style.display = 'none';
document.getElementById(sectionId).style.display = 'block';
}
async function generateKeys() {
try {
const { privateKey, publicKey } = await openpgp.generateKey({
type: 'rsa',
rsaBits: 2048,
userIDs: [{ name: 'User', email: 'user@example.com' }],
passphrase: ''
});
document.getElementById('publicKey').value = publicKey;
document.getElementById('privateKey').value = privateKey;
} catch (error) {
console.error("Key generation failed:", error);
}
}
async function encryptMessage() {
const message = document.getElementById('messageToEncrypt').value;
const publicKeyArmored = document.getElementById('encryptPublicKey').value;
if (!message || !publicKeyArmored) {
alert("Please enter both the message and the public key.");
return;
}
try {
const publicKey = await openpgp.readKey({ armoredKey: publicKeyArmored });
const encrypted = await openpgp.encrypt({
message: await openpgp.createMessage({ text: message }),
encryptionKeys: publicKey
});
document.getElementById('encryptedMessage').value = encrypted;
} catch (error) {
console.error("Encryption failed:", error);
alert("Encryption failed. Please check the public key format.");
}
}
async function decryptMessage() {
const encryptedMessage = document.getElementById('messageToDecrypt').value;
const privateKeyArmored = document.getElementById('decryptPrivateKey').value;
if (!encryptedMessage || !privateKeyArmored) {
alert("Please enter both the encrypted message and the private key.");
return;
}
try {
const privateKey = await openpgp.readPrivateKey({ armoredKey: privateKeyArmored });
const message = await openpgp.readMessage({
armoredMessage: encryptedMessage
});
const { data: decrypted } = await openpgp.decrypt({
message,
decryptionKeys: privateKey
});
document.getElementById('decryptedMessage').value = decrypted;
} catch (error) {
console.error("Decryption failed:", error);
alert("Decryption failed. Please check the private key and the message format.");
}
}
function copyToClipboard(elementId) {
const text = document.getElementById(elementId).value;
if (text) {
navigator.clipboard.writeText(text).then(() => {
alert("Text copied to clipboard.");
}).catch(err => {
console.error("Could not copy text:", err);
});
} else {
alert("Nothing to copy!");
}
}