-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
172 lines (128 loc) · 4.73 KB
/
script.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
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
// 1
const hexToBase64 = (hexString) => {
const bytes = [];
for (let i = 0; i < hexString.length; i += 2) {
bytes.push(parseInt(hexString.substr(i, 2), 16));
}
let base64 = '';
const base64Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
for (let i = 0; i < bytes.length; i += 3) {
const byte1 = bytes[i];
const byte2 = bytes[i + 1];
const byte3 = bytes[i + 2];
const char1 = byte1 >> 2;
const char2 = ((byte1 & 3) << 4) | (byte2 >> 4);
const char3 = ((byte2 & 15) << 2) | (byte3 >> 6);
const char4 = byte3 & 63;
base64 += base64Chars.charAt(char1);
base64 += base64Chars.charAt(char2);
base64 += i + 1 < bytes.length ? base64Chars.charAt(char3) : '=';
base64 += i + 2 < bytes.length ? base64Chars.charAt(char4) : '=';
}
return base64;
}
document.getElementById('convertButton').addEventListener('click', () => {
const hexString = document.getElementById('hexInput').value;
const base64String = hexToBase64(hexString);
const base64OutputDiv = document.getElementById('base64Output');
base64OutputDiv.textContent = ("Your base64 string is: " + base64String);
});
// 2
const fixedXOR = (buffer1, buffer2) =>{
// Convert hexadecimal strings to byte arrays
const bytes1 = hexToBytes(buffer1);
const bytes2 = hexToBytes(buffer2);
// Perform XOR operation byte by byte
const resultBytes = [];
for (let i = 0; i < bytes1.length; i++) {
resultBytes.push(bytes1[i] ^ bytes2[i]);
}
// Convert result byte array to hexadecimal string
const resultHex = bytesToHex(resultBytes);
return resultHex;
}
// Helper const to convert hexadecimal string to byte array
const hexToBytes = hexString => {
const bytes = [];
for (let i = 0; i < hexString.length; i += 2) {
bytes.push(parseInt(hexString.substr(i, 2), 16));
}
return bytes;
};
// Helper const to convert byte array to hexadecimal string
const bytesToHex = bytes => {
return bytes.map(byte => byte.toString(16).padStart(2, '0')).join('');
};
// const to calculate XOR and update the result
const calculateXOR = () => {
const input1 = document.getElementById('input1').value;
const input2 = document.getElementById('input2').value;
const result = fixedXOR(input1, input2);
// Update the document with the result
const resultElement = document.getElementById('result');
resultElement.textContent = 'Result: ' + result;
};
// 3
const xorDecrypt = (hexString, key) => {
const buffer = hexToBytes(hexString);
const result = [];
for (let byte of buffer) {
result.push(byte ^ key);
}
return String.fromCharCode(...result);
};
// 4
const scorePlaintext = (text) => {
// Your scoring logic goes here
};
// Now you can define your handleFile function
const handleFile = () => {
const fileInput = document.getElementById('fileInput');
const file = fileInput.files[0];
if (!file) {
alert('Please select a file.');
return;
}
const reader = new FileReader();
reader.onload = function(event) {
const content = event.target.result;
console.log('Content:', content);
const lines = content.split(/\r?\n/);
// console.log('Lines:', lines);
let mostLikelySentence = null;
let maxScore = 0;
for (const line of lines) {
const score = scorePlaintext(line.trim());
console.log('Line:', line.trim(), 'Score:', score);
if (score > maxScore) {
maxScore = score;
mostLikelySentence = line;
}
}
console.log(`Most likely sentence: ${mostLikelySentence}`);
const resultElement = document.getElementById('result2');
resultElement.value = mostLikelySentence;
};
reader.readAsText(file);
};
// 5
const repeatingKeyXOR = (plaintext, key) => {
const encoder = new TextEncoder();
const decoder = new TextDecoder();
const plaintextBytes = encoder.encode(plaintext);
const keyBytes = encoder.encode(key);
const encryptedBytes = [];
for (let i = 0; i < plaintextBytes.length; i++) {
const byte = plaintextBytes[i] ^ keyBytes[i % keyBytes.length];
encryptedBytes.push(byte);
}
// Convert bytes to hexadecimal representation
const hexString = encryptedBytes.map(byte => byte.toString(16).padStart(2, '0')).join('');
return hexString;
};
const encrypt = () => {
const plaintext = document.getElementById('plaintext').value;
const key = document.getElementById('key').value;
const ciphertext = repeatingKeyXOR(plaintext, key);
document.getElementById('ciphertext').value = ciphertext;
};