-
Notifications
You must be signed in to change notification settings - Fork 0
/
script4.html
57 lines (51 loc) · 2.04 KB
/
script4.html
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
<!DOCTYPE html>
<html>
<head>
<title>File Encryption</title>
</head>
<body>
<h1>File Encryption</h1>
<form id="loginForm">
<label for="masterPassword">Enter Master Password:</label>
<input type="password" id="masterPassword" required>
<button type="submit" id="encryptButton">Encrypt Password</button>
</form>
<div id="output"></div>
<script>
document.getElementById("loginForm").addEventListener("submit", async (e) => {
e.preventDefault();
const masterPassword = document.getElementById("masterPassword").value;
const encodedMasterPassword = new TextEncoder().encode(masterPassword);
try {
// Generate a cryptographic key from the master password.
const keyMaterial = await crypto.subtle.importKey(
"raw",
encodedMasterPassword,
{ name: "PBKDF2" },
false,
["deriveKey"]
);
// Derive an encryption key using the generated key material.
const encryptionKey = await crypto.subtle.deriveKey(
{
name: "PBKDF2",
salt: new TextEncoder().encode("salt123"), // Use a unique salt for each user.
iterations: 100000,
hash: "SHA-256",
},
keyMaterial,
{ name: "AES-GCM", length: 256 },
true,
["encrypt", "decrypt"]
);
// For demonstration purposes, display the encrypted password
document.getElementById("output").innerText = "Encrypted Password: " + btoa(
String.fromCharCode(...new Uint8Array(crypto.getRandomValues(new Uint8Array(16))))
);
} catch (error) {
document.getElementById("output").innerText = "Encryption failed.";
}
});
</script>
</body>
</html>