-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
82 lines (73 loc) · 2.8 KB
/
Copy pathscript.js
File metadata and controls
82 lines (73 loc) · 2.8 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
const keyEl = document.getElementById('key');
const ivEl = document.getElementById('iv');
const inputEl = document.getElementById('input');
const outputEl = document.getElementById('output');
const modeEls = document.querySelectorAll('input[name="mode"]');
const toUtf8 = s => new TextEncoder().encode(s);
const fromUtf8 = u => new TextDecoder().decode(u);
const hexToU8 = h => {
h = h.replace(/^0x/i,'');
if (h.length % 2) h = '0' + h;
const out = new Uint8Array(h.length / 2);
for (let i = 0; i < out.length; i++) out[i] = parseInt(h.substr(i*2,2), 16);
return out;
};
const b64ToU8 = b => {
try {
const bin = atob(b);
const u = new Uint8Array(bin.length);
for (let i = 0; i < bin.length; i++) u[i] = bin.charCodeAt(i);
return u;
} catch { return null; }
};
const u8ToB64 = u => {
let s = '';
for (let i = 0; i < u.length; i++) s += String.fromCharCode(u[i]);
return btoa(s);
};
const parseInput = s => {
if (!s) return null;
s = s.trim();
if (s.startsWith('0x') || s.startsWith('0X')) return hexToU8(s);
if (s.startsWith('b64:')) return b64ToU8(s.slice(4));
return toUtf8(s);
};
const importKey = async raw => {
try {
return await crypto.subtle.importKey('raw', raw, { name: 'AES-CBC' }, false, ['encrypt','decrypt']);
} catch (e) { return null; }
};
let currentMode = 'encrypt';
modeEls.forEach(r => r.addEventListener('change', e => { currentMode = e.target.value; compute(); }));
[keyEl, ivEl, inputEl].forEach(el => el.addEventListener('input', compute));
async function compute() {
outputEl.value = '';
try {
const keyRaw = parseInput(keyEl.value);
const ivRaw = parseInput(ivEl.value);
if (!keyRaw || !ivRaw) return;
if (!(ivRaw instanceof Uint8Array) || ivRaw.length !== 16) { outputEl.value = 'ERROR: IV'; return; }
if (!(keyRaw instanceof Uint8Array) || ![16,24,32].includes(keyRaw.length)) { outputEl.value = 'ERROR: KEY'; return; }
const key = await importKey(keyRaw);
if (!key) { outputEl.value = 'ERROR: impossibile importare la chiave'; return; }
if (currentMode === 'encrypt') {
if (!inputEl.value) return;
const pt = toUtf8(inputEl.value);
const ct = await crypto.subtle.encrypt({ name:'AES-CBC', iv: ivRaw }, key, pt);
outputEl.value = u8ToB64(new Uint8Array(ct));
} else {
if (!inputEl.value) return;
const cipher = b64ToU8(inputEl.value.trim());
if (!cipher) { outputEl.value = 'ERROR: input per decifrare deve essere Base64 valido'; return; }
try {
const ptBuf = await crypto.subtle.decrypt({ name:'AES-CBC', iv: ivRaw }, key, cipher);
outputEl.value = fromUtf8(new Uint8Array(ptBuf));
} catch (e) {
outputEl.value = 'ERROR: decifratura fallita';
}
}
} catch (e) {
outputEl.value = 'ERROR: ' + (e && e.message ? e.message : String(e));
}
}
compute();