-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaes-encrypt.php
More file actions
188 lines (158 loc) · 7.11 KB
/
aes-encrypt.php
File metadata and controls
188 lines (158 loc) · 7.11 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
<!DOCTYPE html>
<html lang="it">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AES Encrypt/Decrypt - Utility Hub</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div class="container">
<a href="index.php" class="back-link">← Torna alla Home</a>
<div class="tool-page">
<h1>🔐 AES Encrypt/Decrypt</h1>
<div class="form-group">
<label for="mode">Modalità</label>
<select id="mode" onchange="toggleMode()">
<option value="encrypt">🔒 Cripta</option>
<option value="decrypt">🔓 Decripta</option>
</select>
</div>
<div class="form-group">
<label for="input">Testo da <span id="action-label">criptare</span></label>
<textarea id="input" placeholder="Inserisci il testo..." rows="6"></textarea>
</div>
<div class="form-group">
<label for="password">Chiave segreta (password)</label>
<input type="text" id="password" placeholder="Inserisci una password sicura">
<small style="color: #888; display: block; margin-top: 6px;">Usa una password di almeno 8 caratteri</small>
</div>
<div class="form-group">
<label for="algorithm">Algoritmo</label>
<select id="algorithm">
<option value="AES-GCM">AES-GCM (consigliato)</option>
<option value="AES-CBC">AES-CBC</option>
</select>
</div>
<button onclick="process()" id="process-btn">🔒 Cripta</button>
<div class="result" id="result" style="display: none;">
<div class="result-label">Risultato</div>
<textarea id="output" readonly rows="6" style="font-family: monospace; font-size: 0.85rem;"></textarea>
<button class="copy-btn" onclick="copy()">📋 Copia</button>
</div>
<div style="margin-top: 20px; padding: 16px; background: #fff3cd; border-radius: 6px; font-size: 0.85rem; color: #856404;">
⚠️ <strong>Attenzione:</strong> La crittografia avviene nel tuo browser. Conserva la password in un luogo sicuro: senza di essa non potrai recuperare il testo decriptato.
</div>
</div>
</div>
<script>
function toggleMode() {
const mode = document.getElementById('mode').value;
const actionLabel = document.getElementById('action-label');
const processBtn = document.getElementById('process-btn');
if (mode === 'encrypt') {
actionLabel.textContent = 'criptare';
processBtn.textContent = '🔒 Cripta';
} else {
actionLabel.textContent = 'decriptare';
processBtn.textContent = '🔓 Decripta';
}
}
async function generateKey(password, salt) {
const enc = new TextEncoder();
const keyMaterial = await crypto.subtle.importKey(
'raw',
enc.encode(password),
'PBKDF2',
false,
['deriveBits', 'deriveKey']
);
return crypto.subtle.deriveKey(
{
name: 'PBKDF2',
salt: enc.encode(salt),
iterations: 100000,
hash: 'SHA-256'
},
keyMaterial,
{ name: 'AES-GCM', length: 256 },
false,
['encrypt', 'decrypt']
);
}
async function encrypt() {
const input = document.getElementById('input').value;
const password = document.getElementById('password').value;
if (!input || !password) {
alert('Inserisci testo e password');
return;
}
try {
const enc = new TextEncoder();
const salt = crypto.getRandomValues(new Uint8Array(16));
const iv = crypto.getRandomValues(new Uint8Array(12));
const key = await generateKey(password, 'salt-' + Array.from(salt).join(''));
const encrypted = await crypto.subtle.encrypt(
{ name: 'AES-GCM', iv: iv },
key,
enc.encode(input)
);
// Combine salt + iv + encrypted data
const combined = new Uint8Array(salt.length + iv.length + encrypted.byteLength);
combined.set(salt, 0);
combined.set(iv, salt.length);
combined.set(new Uint8Array(encrypted), salt.length + iv.length);
// Convert to base64
const base64 = btoa(String.fromCharCode(...combined));
document.getElementById('output').value = base64;
document.getElementById('result').style.display = 'block';
} catch (error) {
alert('Errore durante la crittografia: ' + error.message);
}
}
async function decrypt() {
const input = document.getElementById('input').value.trim();
const password = document.getElementById('password').value;
if (!input || !password) {
alert('Inserisci testo criptato e password');
return;
}
try {
// Decode from base64
const combined = Uint8Array.from(atob(input), c => c.charCodeAt(0));
// Extract salt, iv and encrypted data
const salt = combined.slice(0, 16);
const iv = combined.slice(16, 28);
const encrypted = combined.slice(28);
const key = await generateKey(password, 'salt-' + Array.from(salt).join(''));
const decrypted = await crypto.subtle.decrypt(
{ name: 'AES-GCM', iv: iv },
key,
encrypted
);
const dec = new TextDecoder();
document.getElementById('output').value = dec.decode(decrypted);
document.getElementById('result').style.display = 'block';
} catch (error) {
alert('Errore durante la decriptazione. Password errata o dati corrotti.');
}
}
function process() {
const mode = document.getElementById('mode').value;
if (mode === 'encrypt') {
encrypt();
} else {
decrypt();
}
}
function copy() {
const output = document.getElementById('output');
output.select();
document.execCommand('copy');
const btn = document.querySelector('.copy-btn');
btn.textContent = '✅ Copiato!';
setTimeout(() => btn.textContent = '📋 Copia', 2000);
}
</script>
</body>
</html>