-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
142 lines (119 loc) · 4.99 KB
/
index.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Encrypt / Decrypt File (Web Crypto API)</title>
</head>
<body>
<h1>Encrypt / Decrypt a File</h1>
<input type="file" id="fileInput" accept=".txt, .zip"><br><br>
<label for="key">Enter Key (Password):</label>
<input id="key" placeholder="Enter encryption/decryption key"><br><br>
<button onclick="encryptFile()">Encrypt File</button>
<button onclick="decryptFile()">Decrypt File</button>
<h2>Result:</h2>
<pre id="output"></pre>
<script>
let fileContent = "";
let fileName = ""; // To track original file name
// Read the file content when uploaded
document.getElementById('fileInput').addEventListener('change', function (event) {
const file = event.target.files[0];
fileName = file.name; // Save the file name
const reader = new FileReader();
reader.onload = function (e) {
fileContent = e.target.result; // Reading content as binary
};
reader.readAsArrayBuffer(file); // Important to read as ArrayBuffer
});
// Generate a key from the password
async function generateKey(password, salt) {
const enc = new TextEncoder();
const keyMaterial = await window.crypto.subtle.importKey(
"raw",
enc.encode(password),
{ name: "PBKDF2" },
false,
["deriveKey"]
);
return window.crypto.subtle.deriveKey(
{
name: "PBKDF2",
salt: salt,
iterations: 100000,
hash: "SHA-256"
},
keyMaterial,
{ name: "AES-GCM", length: 256 },
false,
["encrypt", "decrypt"]
);
}
// Encrypt the file content
async function encryptFile() {
const password = document.getElementById('key').value;
if (!fileContent || !password) {
alert("Please upload a file and enter a key.");
return;
}
const salt = window.crypto.getRandomValues(new Uint8Array(16));
const iv = window.crypto.getRandomValues(new Uint8Array(12)); // Initialization vector for AES-GCM
const key = await generateKey(password, salt);
const encryptedContent = await window.crypto.subtle.encrypt(
{
name: "AES-GCM",
iv: iv
},
key,
fileContent // Content is already in ArrayBuffer format
);
// Concatenate salt, iv, and encrypted content into one Uint8Array for easy storage
const result = new Uint8Array(salt.length + iv.length + encryptedContent.byteLength);
result.set(salt);
result.set(iv, salt.length);
result.set(new Uint8Array(encryptedContent), salt.length + iv.length);
document.getElementById('output').textContent = "File encrypted successfully!";
downloadFile(result, 'encrypted_' + fileName); // Keep original extension
}
// Decrypt the file content
async function decryptFile() {
const password = document.getElementById('key').value;
if (!fileContent || !password) {
alert("Please upload a file and enter a key.");
return;
}
// Extract salt and IV from the beginning of the file content
const fileData = new Uint8Array(fileContent);
const salt = fileData.slice(0, 16);
const iv = fileData.slice(16, 28);
const encryptedContent = fileData.slice(28);
const key = await generateKey(password, salt);
try {
const decryptedContent = await window.crypto.subtle.decrypt(
{
name: "AES-GCM",
iv: iv
},
key,
encryptedContent
);
document.getElementById('output').textContent = "File decrypted successfully!";
downloadFile(new Uint8Array(decryptedContent), 'decrypted_' + fileName);
} catch (e) {
console.error(e);
alert("Decryption failed. Check your key or file.");
}
}
// Function to download the result
function downloadFile(content, fileName) {
const element = document.createElement('a');
const file = new Blob([content], { type: 'application/octet-stream' }); // Generic binary stream
element.href = URL.createObjectURL(file);
element.download = fileName;
document.body.appendChild(element);
element.click();
}
</script>
</body>
</html>