-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
309 lines (264 loc) · 14 KB
/
Copy pathindex.html
File metadata and controls
309 lines (264 loc) · 14 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>xsukax URL Secure Messenger</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-50 min-h-screen">
<div id="toast-container" class="fixed top-4 right-4 z-50 space-y-2"></div>
<div class="container mx-auto px-4 py-12 max-w-3xl">
<header class="text-center mb-10">
<h1 class="text-4xl font-semibold text-gray-900 mb-3">xsukax URL Secure Messenger</h1>
<p class="text-gray-600">Secure message exchange via AES-256 encryption embedded in URL hashes</p>
</header>
<div id="encrypt-view" class="bg-white rounded-lg shadow-sm border border-gray-200 p-8">
<h2 class="text-xl font-semibold text-gray-900 mb-6">Encrypt Message</h2>
<div class="mb-6">
<label for="message" class="block text-sm font-medium text-gray-700 mb-2">Message</label>
<textarea id="message" rows="6" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent resize-none" placeholder="Enter your secret message..."></textarea>
</div>
<div class="mb-6">
<label for="password" class="block text-sm font-medium text-gray-700 mb-2">Password</label>
<input type="password" id="password" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent" placeholder="Enter encryption password">
<p class="mt-2 text-sm text-gray-500">Use a strong password. It will be needed to decrypt the message.</p>
</div>
<button id="generate-btn" class="w-full bg-blue-600 hover:bg-blue-700 text-white font-medium py-2.5 px-4 rounded-md transition-colors focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2">
Generate Secure Link
</button>
</div>
<div id="result-view" class="hidden bg-white rounded-lg shadow-sm border border-gray-200 p-8">
<h2 class="text-xl font-semibold text-gray-900 mb-6">Secure Link Generated</h2>
<div class="mb-6">
<label class="block text-sm font-medium text-gray-700 mb-2">Encrypted URL</label>
<div class="flex">
<input type="text" id="encrypted-url" readonly class="flex-1 px-3 py-2 bg-gray-50 border border-gray-300 rounded-l-md text-gray-700 text-sm">
<button id="copy-btn" class="bg-gray-700 hover:bg-gray-800 text-white px-4 py-2 rounded-r-md transition-colors focus:outline-none focus:ring-2 focus:ring-gray-500 focus:ring-offset-2">
Copy
</button>
</div>
</div>
<div class="flex gap-3">
<a id="test-link-btn" href="#" target="_blank" class="flex-1 bg-green-600 hover:bg-green-700 text-white font-medium py-2.5 px-4 rounded-md transition-colors focus:outline-none focus:ring-2 focus:ring-green-500 focus:ring-offset-2 text-center">
Test Link
</a>
<button id="new-msg-btn" class="flex-1 bg-gray-200 hover:bg-gray-300 text-gray-900 font-medium py-2.5 px-4 rounded-md transition-colors focus:outline-none focus:ring-2 focus:ring-gray-500 focus:ring-offset-2">
Create New Message
</button>
</div>
</div>
<div id="decrypt-view" class="hidden bg-white rounded-lg shadow-sm border border-gray-200 p-8">
<h2 class="text-xl font-semibold text-gray-900 mb-6">Decrypt Message</h2>
<div class="mb-6">
<label for="decrypt-password" class="block text-sm font-medium text-gray-700 mb-2">Password</label>
<input type="password" id="decrypt-password" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent" placeholder="Enter decryption password">
</div>
<button id="decrypt-btn" class="w-full bg-blue-600 hover:bg-blue-700 text-white font-medium py-2.5 px-4 rounded-md transition-colors focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 mb-4">
Show Message
</button>
<div id="decrypted-message" class="hidden">
<label class="block text-sm font-medium text-gray-700 mb-2">Decrypted Message</label>
<div class="bg-gray-50 border border-gray-200 rounded-md p-4 mb-4">
<p id="message-content" class="text-gray-900 whitespace-pre-wrap"></p>
</div>
<div class="flex gap-3">
<button id="download-btn" class="flex-1 bg-blue-600 hover:bg-blue-700 text-white font-medium py-2.5 px-4 rounded-md transition-colors focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2">
Download as Text File
</button>
<button id="create-new-btn" class="flex-1 bg-gray-200 hover:bg-gray-300 text-gray-900 font-medium py-2.5 px-4 rounded-md transition-colors focus:outline-none focus:ring-2 focus:ring-gray-500 focus:ring-offset-2">
Create New Message
</button>
</div>
</div>
</div>
<div class="mt-10 bg-blue-50 border border-blue-200 rounded-lg p-6">
<h3 class="text-sm font-semibold text-blue-900 mb-3">Security Information</h3>
<ul class="text-sm text-blue-800 space-y-2">
<li>• All encryption happens locally in your browser using AES-256-GCM</li>
<li>• Messages are never sent to any server</li>
<li>• The encrypted data is embedded in the URL hash</li>
<li>• Share the URL and password through separate secure channels</li>
</ul>
</div>
</div>
<script>
function showToast(message, type = 'success') {
const container = document.getElementById('toast-container');
const toast = document.createElement('div');
const bgColor = type === 'success' ? 'bg-green-600' : 'bg-red-600';
toast.className = `${bgColor} text-white px-4 py-3 rounded-md shadow-lg transform transition-all duration-300 ease-in-out translate-x-full opacity-0`;
toast.textContent = message;
container.appendChild(toast);
setTimeout(() => {
toast.classList.remove('translate-x-full', 'opacity-0');
}, 10);
setTimeout(() => {
toast.classList.add('translate-x-full', 'opacity-0');
setTimeout(() => toast.remove(), 300);
}, 3000);
}
function arrayBufferToBase64(buffer) {
const bytes = new Uint8Array(buffer);
let binary = '';
for (let i = 0; i < bytes.byteLength; i++) {
binary += String.fromCharCode(bytes[i]);
}
return btoa(binary).replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
}
function base64ToArrayBuffer(base64) {
base64 = base64.replace(/-/g, '+').replace(/_/g, '/');
while (base64.length % 4) {
base64 += '=';
}
const binary = atob(base64);
const bytes = new Uint8Array(binary.length);
for (let i = 0; i < binary.length; i++) {
bytes[i] = binary.charCodeAt(i);
}
return bytes.buffer;
}
async function deriveKey(password, salt) {
const encoder = new TextEncoder();
const keyMaterial = await crypto.subtle.importKey(
'raw',
encoder.encode(password),
'PBKDF2',
false,
['deriveBits', 'deriveKey']
);
return crypto.subtle.deriveKey(
{
name: 'PBKDF2',
salt: salt,
iterations: 100000,
hash: 'SHA-256'
},
keyMaterial,
{ name: 'AES-GCM', length: 256 },
false,
['encrypt', 'decrypt']
);
}
async function encryptMessage(message, password) {
const encoder = new TextEncoder();
const salt = crypto.getRandomValues(new Uint8Array(16));
const iv = crypto.getRandomValues(new Uint8Array(12));
const key = await deriveKey(password, salt);
const encrypted = await crypto.subtle.encrypt(
{ name: 'AES-GCM', iv: iv },
key,
encoder.encode(message)
);
const combined = new Uint8Array(salt.length + iv.length + encrypted.byteLength);
combined.set(salt, 0);
combined.set(iv, 16);
combined.set(new Uint8Array(encrypted), 28);
return arrayBufferToBase64(combined.buffer);
}
async function decryptMessage(encryptedData, password) {
const combined = new Uint8Array(base64ToArrayBuffer(encryptedData));
const salt = combined.slice(0, 16);
const iv = combined.slice(16, 28);
const encrypted = combined.slice(28);
const key = await deriveKey(password, salt);
const decrypted = await crypto.subtle.decrypt(
{ name: 'AES-GCM', iv: iv },
key,
encrypted
);
return new TextDecoder().decode(decrypted);
}
function switchView(view) {
document.getElementById('encrypt-view').classList.add('hidden');
document.getElementById('result-view').classList.add('hidden');
document.getElementById('decrypt-view').classList.add('hidden');
document.getElementById(view).classList.remove('hidden');
}
function checkHash() {
const hash = window.location.hash.substring(1);
if (hash) {
switchView('decrypt-view');
} else {
switchView('encrypt-view');
}
}
document.getElementById('generate-btn').addEventListener('click', async () => {
const message = document.getElementById('message').value.trim();
const password = document.getElementById('password').value;
if (!message) {
showToast('Please enter a message', 'error');
return;
}
if (!password) {
showToast('Please enter a password', 'error');
return;
}
if (password.length < 6) {
showToast('Password must be at least 6 characters', 'error');
return;
}
try {
const encrypted = await encryptMessage(message, password);
const url = `${window.location.origin}${window.location.pathname}#${encrypted}`;
document.getElementById('encrypted-url').value = url;
document.getElementById('test-link-btn').href = url;
switchView('result-view');
showToast('Message encrypted successfully');
} catch (error) {
showToast('Encryption failed', 'error');
}
});
document.getElementById('copy-btn').addEventListener('click', () => {
const input = document.getElementById('encrypted-url');
input.select();
navigator.clipboard.writeText(input.value).then(() => {
showToast('Link copied to clipboard');
});
});
document.getElementById('new-msg-btn').addEventListener('click', () => {
window.location.hash = '';
document.getElementById('message').value = '';
document.getElementById('password').value = '';
switchView('encrypt-view');
});
document.getElementById('decrypt-btn').addEventListener('click', async () => {
const password = document.getElementById('decrypt-password').value;
const hash = window.location.hash.substring(1);
if (!password) {
showToast('Please enter the password', 'error');
return;
}
try {
const decrypted = await decryptMessage(hash, password);
document.getElementById('message-content').textContent = decrypted;
document.getElementById('decrypted-message').classList.remove('hidden');
showToast('Message decrypted successfully');
} catch (error) {
showToast('Decryption failed. Wrong password?', 'error');
}
});
document.getElementById('create-new-btn').addEventListener('click', () => {
window.location.hash = '';
document.getElementById('decrypt-password').value = '';
document.getElementById('decrypted-message').classList.add('hidden');
switchView('encrypt-view');
});
document.getElementById('download-btn').addEventListener('click', () => {
const messageContent = document.getElementById('message-content').textContent;
const blob = new Blob([messageContent], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `decrypted-message-${Date.now()}.txt`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
showToast('Message downloaded successfully');
});
window.addEventListener('hashchange', checkHash);
checkHash();
</script>
</body>
</html>