-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
151 lines (134 loc) · 5.65 KB
/
Copy pathscript.js
File metadata and controls
151 lines (134 loc) · 5.65 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
document.addEventListener('DOMContentLoaded', function() {
document.getElementById('uploadKey').addEventListener('change', handleFileUpload);
const clearKeyButton = document.getElementById('clearKeyButton');
if (clearKeyButton) {
clearKeyButton.addEventListener('click', clearKey);
}
updateUIBasedOnKeyPresence();
});
function handleFileUpload(event) {
const file = event.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = function(e) {
const key = e.target.result;
sessionStorage.setItem('key', btoa(key)); // Encode and store key in sessionStorage
displayMessage("Key file uploaded and stored successfully.", "bg-green-500 px-4 py-2 rounded");
updateUIBasedOnKeyPresence();
};
reader.onerror = function () {
displayMessage("Failed to read key file.", "bg-red-500 px-4 py-2 rounded");
};
reader.readAsText(file);
}
function clearKey() {
sessionStorage.removeItem('key');
displayMessage("Key cleared from storage.", "bg-yellow-500 px-4 py-2 rounded");
updateUIBasedOnKeyPresence();
}
function updateUIBasedOnKeyPresence() {
const key = sessionStorage.getItem('key');
const clearButton = document.getElementById('clearKeyButton');
const keySensitiveUI = document.getElementById('keySensitiveUI');
if (key) {
clearButton.classList.remove('hidden');
keySensitiveUI.classList.remove('hidden');
} else {
clearButton.classList.add('hidden');
keySensitiveUI.classList.add('hidden');
}
}
function displayMessage(message, classes) {
const messageElement = document.getElementById('message');
messageElement.textContent = message;
messageElement.className = classes;
setTimeout(() => {
messageElement.classList.add('hidden');
}, 5000);
}
async function encryptAndCopy() {
const password = document.getElementById('password').value;
const data = document.getElementById('textArea').value;
if (!password || !data) {
displayMessage("Password and data are required for encryption.", "bg-red-500 px-4 py-2 rounded");
return;
}
try {
const keyMaterial = await getKeyMaterial(password);
const key = await window.crypto.subtle.deriveKey(
{ name: "PBKDF2", salt: new Uint8Array(sessionStorage.getItem('key').split(',')), iterations: 100000, hash: 'SHA-256' },
keyMaterial,
{ name: "AES-GCM", length: 256 },
false,
["encrypt", "decrypt"]
);
const iv = window.crypto.getRandomValues(new Uint8Array(12));
const encoded = new TextEncoder().encode(data);
const encrypted = await window.crypto.subtle.encrypt({ name: "AES-GCM", iv }, key, encoded);
const encryptedData = [...iv, ...new Uint8Array(encrypted)];
const base64Encrypted = btoa(String.fromCharCode(...encryptedData));
document.getElementById('textArea').value = base64Encrypted;
await navigator.clipboard.writeText(base64Encrypted);
displayMessage("Text encrypted and copied to clipboard!", "bg-green-500 px-4 py-2 rounded");
} catch (error) {
displayMessage("Encryption failed: " + error.message, "bg-red-500 px-4 py-2 rounded");
}
}
async function decryptText() {
const password = document.getElementById('password').value;
const base64Encrypted = document.getElementById('textArea').value;
if (!password || !base64Encrypted) {
displayMessage("Password and encrypted data are required for decryption.", "bg-red-500 px-4 py-2 rounded");
return;
}
try {
const keyMaterial = await getKeyMaterial(password);
const key = await window.crypto.subtle.deriveKey(
{ name: "PBKDF2", salt: new Uint8Array(sessionStorage.getItem('key').split(',')), iterations: 100000, hash: 'SHA-256' },
keyMaterial,
{ name: "AES-GCM", length: 256 },
false,
["decrypt"]
);
const encryptedBytes = Uint8Array.from(atob(base64Encrypted), c => c.charCodeAt(0));
const iv = encryptedBytes.slice(0, 12);
const encrypted = encryptedBytes.slice(12);
const decrypted = await window.crypto.subtle.decrypt({ name: "AES-GCM", iv }, key, encrypted);
const dec = new TextDecoder().decode(decrypted);
document.getElementById('textArea').value = dec;
displayMessage("Text decrypted successfully!", "bg-green-500 px-4 py-2 rounded");
} catch (error) {
displayMessage("Decryption failed: " + error.message, "bg-red-500 px-4 py-2 rounded");
}
}
function getKeyMaterial(password) {
const enc = new TextEncoder();
return window.crypto.subtle.importKey(
"raw",
enc.encode(password),
{ name: "PBKDF2" },
false,
["deriveBits", "deriveKey"]
);
}
function generateKey() {
const password = document.getElementById('keyPassword').value;
const salt = crypto.getRandomValues(new Uint8Array(16));
console.log("Generating key with password and salt.");
const key = window.btoa(String.fromCharCode(...salt));
displayMessage("Key file generated and ready for download.", "bg-green-500 px-4 py-2 rounded");
const blob = new Blob([key], {type: 'text/plain'});
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'cloak.key';
a.click();
window.URL.revokeObjectURL(url);
}
function showTab(tabId) {
const tabs = document.getElementsByClassName('tab-content');
for (let i = 0; i < tabs.length; i++) {
tabs[i].style.display = 'none';
}
document.getElementById(tabId).style.display = 'block';
}