-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
784 lines (685 loc) · 24.7 KB
/
Copy pathindex.html
File metadata and controls
784 lines (685 loc) · 24.7 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>XOR Tool - Web Version</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
padding: 20px;
display: flex;
justify-content: center;
align-items: center;
}
.container {
background: white;
border-radius: 12px;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
padding: 40px;
max-width: 900px;
width: 100%;
}
h1 {
color: #333;
margin-bottom: 10px;
font-size: 28px;
}
.subtitle {
color: #666;
margin-bottom: 30px;
font-size: 14px;
}
.info-box {
background: #f0f4ff;
border-left: 4px solid #667eea;
padding: 15px;
margin-bottom: 30px;
border-radius: 4px;
}
.info-box h3 {
color: #667eea;
font-size: 14px;
margin-bottom: 8px;
}
.info-box p {
color: #555;
font-size: 13px;
line-height: 1.6;
}
.info-box code {
background: #e0e7ff;
padding: 2px 6px;
border-radius: 3px;
font-family: 'Courier New', monospace;
font-size: 12px;
}
.input-section {
margin-bottom: 20px;
}
label {
display: block;
margin-bottom: 8px;
color: #444;
font-weight: 600;
font-size: 14px;
}
textarea {
width: 100%;
padding: 12px;
border: 2px solid #e0e0e0;
border-radius: 6px;
font-family: 'Courier New', monospace;
font-size: 14px;
resize: vertical;
transition: border-color 0.3s;
}
textarea:focus {
outline: none;
border-color: #667eea;
}
.button-container {
text-align: center;
margin: 30px 0;
}
button {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
padding: 14px 40px;
font-size: 16px;
font-weight: 600;
border-radius: 6px;
cursor: pointer;
transition: transform 0.2s, box-shadow 0.2s;
box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
}
button:hover {
transform: translateY(-2px);
box-shadow: 0 6px 20px rgba(102, 126, 234, 0.6);
}
button:active {
transform: translateY(0);
}
.result-section {
background: #f9fafb;
border: 2px solid #e0e0e0;
border-radius: 6px;
padding: 15px;
}
.result-section h3 {
color: #444;
font-size: 14px;
margin-bottom: 10px;
}
#result {
font-family: 'Courier New', monospace;
font-size: 14px;
color: #333;
white-space: pre-wrap;
word-break: break-all;
min-height: 60px;
line-height: 1.6;
}
.result-empty {
color: #999;
font-style: italic;
}
.example-section {
margin-top: 30px;
padding-top: 30px;
border-top: 1px solid #e0e0e0;
}
.example-section h3 {
color: #444;
font-size: 16px;
margin-bottom: 10px;
}
.example-section p {
color: #666;
font-size: 13px;
line-height: 1.6;
}
.example-buttons {
margin-top: 15px;
display: flex;
gap: 10px;
flex-wrap: wrap;
}
.example-btn {
background: #f0f4ff;
color: #667eea;
border: 1px solid #667eea;
padding: 8px 16px;
font-size: 13px;
border-radius: 4px;
cursor: pointer;
transition: all 0.2s;
}
.example-btn:hover {
background: #667eea;
color: white;
}
.input-wrapper {
position: relative;
}
.format-badge {
display: inline-block;
background: #10b981;
color: white;
padding: 4px 10px;
border-radius: 12px;
font-size: 11px;
font-weight: 600;
margin-left: 8px;
text-transform: uppercase;
letter-spacing: 0.5px;
}
.format-badge.base64 {
background: #3b82f6;
}
.result-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 10px;
}
.result-title {
color: #444;
font-size: 14px;
font-weight: 600;
}
.copy-btn {
background: #667eea;
color: white;
border: none;
padding: 6px 12px;
font-size: 12px;
border-radius: 4px;
cursor: pointer;
transition: background 0.2s;
}
.copy-btn:hover {
background: #5568d3;
}
.copy-btn:active {
transform: none;
}
.copy-btn.copied {
background: #10b981;
}
.random-btn {
display: none;
background: #f0f4ff;
color: #667eea;
border: 1px solid #667eea;
padding: 4px 10px;
font-size: 11px;
border-radius: 4px;
cursor: pointer;
margin-left: 10px;
transition: all 0.2s;
font-weight: 600;
}
.random-btn:hover {
background: #667eea;
color: white;
}
.random-btn:active {
transform: none;
}
.random-btn.visible {
display: inline-block;
}
</style>
</head>
<body>
<div class="container">
<h1>XOR Tool - Web Version</h1>
<p class="subtitle">XOR two strings together with automatic zero-padding and stripping</p>
<div class="info-box">
<h3>XOR Properties & Smart Encoding</h3>
<p>
If <code>result = A ⊕ B</code>, then <code>A = result ⊕ B</code> and <code>B = result ⊕ A</code>
<br>
This means any two of the three values can generate the third. The XOR operation is symmetric and reversible.
</p>
<p style="margin-top: 10px;">
<strong>Automatic Base64 handling:</strong> Input is auto-detected as base64 or text. Results are shown as text when possible,
otherwise as base64. You can copy base64 results and paste them back as input - they'll be automatically decoded!
</p>
</div>
<div class="input-section">
<label for="input1">
String 1:
<span id="badge1" class="format-badge" style="display: none;"></span>
<button id="random-btn1" class="random-btn" onclick="generateRandomKey(1)">🎲 Random Key</button>
</label>
<textarea id="input1" rows="4" placeholder="Enter first string (text or base64)..."></textarea>
</div>
<div class="input-section">
<label for="input2">
String 2:
<span id="badge2" class="format-badge" style="display: none;"></span>
<button id="random-btn2" class="random-btn" onclick="generateRandomKey(2)">🎲 Random Key</button>
</label>
<textarea id="input2" rows="4" placeholder="Enter second string (text or base64)..."></textarea>
</div>
<div class="button-container">
<button onclick="performXOR()">XOR Strings</button>
</div>
<div class="result-section">
<div class="result-header">
<div class="result-title">
Result:
<span id="result-badge" class="format-badge" style="display: none;"></span>
</div>
<button id="copy-btn" class="copy-btn" onclick="copyResult()" style="display: none;">Copy</button>
</div>
<div id="result" class="result-empty">XOR result will appear here...</div>
</div>
<div class="example-section">
<h3>Quick Examples</h3>
<p>Try these example scenarios to see how XOR works:</p>
<div class="example-buttons">
<button class="example-btn" onclick="loadExample1()">Example 1: Encrypt/Decrypt</button>
<button class="example-btn" onclick="loadExample2()">Example 2: Key Recovery</button>
<button class="example-btn" onclick="clearAll()">Clear All</button>
</div>
</div>
</div>
<script>
// Store the current result for copying
let currentResult = '';
/**
* Strict base64 validation
* Returns true only if the string is valid base64 with correct padding and can be decoded
*/
function isValidBase64(str) {
// Empty string is not base64
if (!str || str.trim() === '') {
return false;
}
// Remove whitespace
str = str.trim();
// Check length is multiple of 4
if (str.length % 4 !== 0) {
return false;
}
// Base64 regex: only valid base64 characters
const base64Regex = /^[A-Za-z0-9+/]*={0,2}$/;
if (!base64Regex.test(str)) {
return false;
}
// Check padding rules
const paddingCount = (str.match(/=/g) || []).length;
if (paddingCount > 2) {
return false;
}
// Padding can only be at the end
const firstPadIndex = str.indexOf('=');
if (firstPadIndex !== -1 && firstPadIndex !== str.length - paddingCount) {
return false;
}
// Try to decode - if it fails, it's not valid base64
try {
const decoded = atob(str);
// Successfully decoded
return true;
} catch (e) {
return false;
}
}
/**
* Convert a string to a Uint8Array (UTF-8 encoding)
*/
function stringToBytes(str) {
const encoder = new TextEncoder();
return encoder.encode(str);
}
/**
* Convert a Uint8Array to a string (UTF-8 decoding)
* Returns null if decoding fails (invalid UTF-8)
*/
function bytesToString(bytes) {
try {
const decoder = new TextDecoder('utf-8', { fatal: true });
return decoder.decode(bytes);
} catch (e) {
return null;
}
}
/**
* Check if byte array contains non-printable characters
* Returns true if there are null bytes, control characters, or other non-printable chars
*/
function hasNonPrintableChars(bytes) {
for (let i = 0; i < bytes.length; i++) {
const byte = bytes[i];
// Null byte
if (byte === 0x00) {
return true;
}
// Control characters (except tab, newline, carriage return)
if (byte < 0x20 && byte !== 0x09 && byte !== 0x0A && byte !== 0x0D) {
return true;
}
// DEL character
if (byte === 0x7F) {
return true;
}
}
return false;
}
/**
* Convert base64 string to Uint8Array
*/
function base64ToBytes(base64Str) {
const binaryString = atob(base64Str);
const bytes = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
return bytes;
}
/**
* Convert Uint8Array to base64 string
*/
function bytesToBase64(bytes) {
let binaryString = '';
for (let i = 0; i < bytes.length; i++) {
binaryString += String.fromCharCode(bytes[i]);
}
return btoa(binaryString);
}
/**
* Process input string: auto-detect and decode base64, or treat as UTF-8 text
* Returns { bytes: Uint8Array, isBase64: boolean }
*/
function processInput(str) {
if (isValidBase64(str)) {
return {
bytes: base64ToBytes(str.trim()),
isBase64: true
};
} else {
return {
bytes: stringToBytes(str),
isBase64: false
};
}
}
/**
* XOR two byte arrays together
* Pads the shorter array with zeros to match the longer one
* Strips trailing zero bytes from the result
*/
function xorBytes(bytes1, bytes2) {
// Determine the maximum length
const maxLen = Math.max(bytes1.length, bytes2.length);
// Create result array
const result = new Uint8Array(maxLen);
// XOR byte by byte, treating missing bytes as zero
for (let i = 0; i < maxLen; i++) {
const b1 = i < bytes1.length ? bytes1[i] : 0;
const b2 = i < bytes2.length ? bytes2[i] : 0;
result[i] = b1 ^ b2;
}
// Strip trailing zero bytes
let endIndex = result.length;
while (endIndex > 0 && result[endIndex - 1] === 0) {
endIndex--;
}
// Return the trimmed result
return result.slice(0, endIndex);
}
/**
* Update format badge for an input
*/
function updateBadge(badgeId, isBase64) {
const badge = document.getElementById(badgeId);
if (isBase64) {
badge.textContent = 'Base64';
badge.className = 'format-badge base64';
badge.style.display = 'inline-block';
} else {
badge.style.display = 'none';
}
}
/**
* Update result badge
*/
function updateResultBadge(format) {
const badge = document.getElementById('result-badge');
if (format === 'text') {
badge.textContent = 'Text';
badge.className = 'format-badge';
badge.style.display = 'inline-block';
} else if (format === 'base64') {
badge.textContent = 'Base64';
badge.className = 'format-badge base64';
badge.style.display = 'inline-block';
} else {
badge.style.display = 'none';
}
}
/**
* Main XOR operation
*/
function performXOR() {
// Get input values
const input1 = document.getElementById('input1').value;
const input2 = document.getElementById('input2').value;
const resultDiv = document.getElementById('result');
const copyBtn = document.getElementById('copy-btn');
// Handle empty inputs
if (!input1 && !input2) {
resultDiv.textContent = 'Please enter at least one string.';
resultDiv.className = 'result-empty';
copyBtn.style.display = 'none';
updateResultBadge(null);
updateBadge('badge1', false);
updateBadge('badge2', false);
return;
}
// Process inputs (auto-detect base64)
const processed1 = processInput(input1);
const processed2 = processInput(input2);
// Update input badges
updateBadge('badge1', processed1.isBase64);
updateBadge('badge2', processed2.isBase64);
// Perform XOR
const resultBytes = xorBytes(processed1.bytes, processed2.bytes);
// Handle empty result
if (resultBytes.length === 0) {
resultDiv.textContent = '(empty - all zeros stripped)';
resultDiv.className = 'result-empty';
copyBtn.style.display = 'none';
updateResultBadge(null);
currentResult = '';
return;
}
// Try to display as text first (only if valid UTF-8 and all printable)
const asText = bytesToString(resultBytes);
const hasBinaryData = hasNonPrintableChars(resultBytes);
if (asText !== null && !hasBinaryData) {
// Valid UTF-8 with all printable characters - display as text
resultDiv.textContent = asText;
resultDiv.className = '';
updateResultBadge('text');
currentResult = asText;
} else {
// Invalid UTF-8 or contains non-printable chars - display as base64
const asBase64 = bytesToBase64(resultBytes);
resultDiv.textContent = asBase64;
resultDiv.className = '';
updateResultBadge('base64');
currentResult = asBase64;
}
// Show copy button
copyBtn.style.display = 'block';
copyBtn.textContent = 'Copy';
copyBtn.className = 'copy-btn';
}
/**
* Copy result to clipboard
*/
function copyResult() {
const copyBtn = document.getElementById('copy-btn');
navigator.clipboard.writeText(currentResult).then(() => {
copyBtn.textContent = 'Copied!';
copyBtn.className = 'copy-btn copied';
setTimeout(() => {
copyBtn.textContent = 'Copy';
copyBtn.className = 'copy-btn';
}, 2000);
}).catch(err => {
console.error('Failed to copy:', err);
copyBtn.textContent = 'Failed';
});
}
/**
* Example 1: Demonstrate encryption and decryption
*/
function loadExample1() {
document.getElementById('input1').value = 'Hello, World!';
document.getElementById('input2').value = 'secret_key';
document.getElementById('result').textContent = 'Click "XOR Strings" to encrypt. Then copy the result and paste it back as input with "secret_key" to decrypt!';
document.getElementById('result').className = 'result-empty';
document.getElementById('copy-btn').style.display = 'none';
updateResultBadge(null);
updateBadge('badge1', false);
updateBadge('badge2', false);
updateRandomButtonVisibility();
}
/**
* Example 2: Demonstrate key recovery
*/
function loadExample2() {
document.getElementById('input1').value = 'plaintext';
document.getElementById('input2').value = 'ciphertext';
document.getElementById('result').textContent = 'Click "XOR Strings" to generate the key. Then XOR the result with either input to recover the other!';
document.getElementById('result').className = 'result-empty';
document.getElementById('copy-btn').style.display = 'none';
updateResultBadge(null);
updateBadge('badge1', false);
updateBadge('badge2', false);
updateRandomButtonVisibility();
}
/**
* Clear all inputs and results
*/
function clearAll() {
document.getElementById('input1').value = '';
document.getElementById('input2').value = '';
document.getElementById('result').textContent = 'XOR result will appear here...';
document.getElementById('result').className = 'result-empty';
document.getElementById('copy-btn').style.display = 'none';
updateResultBadge(null);
updateBadge('badge1', false);
updateBadge('badge2', false);
updateRandomButtonVisibility();
currentResult = '';
}
/**
* Get the byte length of an input string (accounting for base64 decoding)
*/
function getByteLength(str) {
if (!str) return 0;
const processed = processInput(str);
return processed.bytes.length;
}
/**
* Generate cryptographically random bytes
*/
function generateRandomBytes(length) {
const bytes = new Uint8Array(length);
crypto.getRandomValues(bytes);
return bytes;
}
/**
* Generate a random key and fill the target input field
* @param {number} targetField - 1 or 2, indicating which field to fill
*/
function generateRandomKey(targetField) {
const sourceField = targetField === 1 ? 2 : 1;
const sourceInput = document.getElementById(`input${sourceField}`).value;
// Get byte length of source
const byteLength = getByteLength(sourceInput);
if (byteLength === 0) {
return; // Safety check
}
// Generate random bytes
const randomBytes = generateRandomBytes(byteLength);
// Encode as base64
const randomBase64 = bytesToBase64(randomBytes);
// Fill target field
document.getElementById(`input${targetField}`).value = randomBase64;
// Update badge for target field
updateBadge(`badge${targetField}`, true);
// Update button visibility
updateRandomButtonVisibility();
}
/**
* Update visibility of random key buttons based on input state
*/
function updateRandomButtonVisibility() {
const input1 = document.getElementById('input1').value;
const input2 = document.getElementById('input2').value;
const btn1 = document.getElementById('random-btn1');
const btn2 = document.getElementById('random-btn2');
// Show button 1 if input1 is empty and input2 has content
if (!input1 && input2) {
btn1.classList.add('visible');
} else {
btn1.classList.remove('visible');
}
// Show button 2 if input2 is empty and input1 has content
if (!input2 && input1) {
btn2.classList.add('visible');
} else {
btn2.classList.remove('visible');
}
}
// Update badges and random buttons on input change (debounced)
let debounceTimer1, debounceTimer2;
document.getElementById('input1').addEventListener('input', function() {
clearTimeout(debounceTimer1);
debounceTimer1 = setTimeout(() => {
const input = this.value;
if (input) {
updateBadge('badge1', isValidBase64(input));
} else {
updateBadge('badge1', false);
}
updateRandomButtonVisibility();
}, 300);
});
document.getElementById('input2').addEventListener('input', function() {
clearTimeout(debounceTimer2);
debounceTimer2 = setTimeout(() => {
const input = this.value;
if (input) {
updateBadge('badge2', isValidBase64(input));
} else {
updateBadge('badge2', false);
}
updateRandomButtonVisibility();
}, 300);
});
// Allow Enter key to trigger XOR (with Ctrl/Cmd modifier)
document.addEventListener('keydown', function(event) {
if ((event.ctrlKey || event.metaKey) && event.key === 'Enter') {
performXOR();
}
});
</script>
</body>
</html>