-
Notifications
You must be signed in to change notification settings - Fork 0
/
password-image-encrypter.html
412 lines (385 loc) · 10.9 KB
/
password-image-encrypter.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<!DOCTYPE html>
<html>
<head>
<meta http-equiv='Content-Type' charset='utf-8' />
<title>Password Image Encoder/Decoder</title>
<style>
html {
font-family: 'Segoe UI';
}
.group {
float: left;
display: inline-block;
margin: 4px;
padding: 4px;
box-shadow: gray 2px 2px 2px 2px;
}
#passwords-encoded {
}
#passwords-decoded {
}
.empty {
border: 2px dashed gray;
}
.input {
border: 1px solid black;
}
table {
border-collapse: collapse;
}
#encode, #decode {
width: 100%;
height: 30px;
}
h2 {
margin: 6px 4px;
}
thead {
font-weight: bold;
}
td, th {
border: 1px solid #999;
padding: 2px;
text-align: left;
}
input[type=textbox]:not(#key) {
border: none;
}
canvas {
margin: 10px;
}
#error {
color: red;
font-size: 14px;
}
.hidden {
display: none;
}
</style>
</head>
<body>
<center><h1>Password Image Encoder/Decoder</h1></center>
<div class='group' id='passwords-decoded'>
<h2>Password Table</h2>
<div class='file-io'>
<button onclick='saveJson()' title='Save as a .json file'>Save</button>
<input id='file-json' type='file' onchange='loadJson(this.files[0])'><br>
<span id='error' class='hidden'></span>
</div>
<table id='passwords'>
<thead>
<tr>
<td style='width:120px;'>Website</td>
<td style='width:120px;'>Username</td>
<td style='width:120px;'>Password</td>
<td style='width:120px;'>Email</td>
<td style='width:120px;'>Other Data</td>
<td style='width:120px;'>Actions</td>
</tr>
</thead>
<tfoot>
<tr>
<td colspan='5'><i>All data is stored locally on your computer. Do not share your key with anyone.</i></td>
<td>
<button id='add' title='Add a password' onclick='addRecord()'>Add</button>
<button id='clear' title='Clear all passwords' onclick='clearTable()'>Clear</button>
</td>
</tr>
</tfoot>
<tbody>
</tbody>
</table>
</div>
<div class='group' id='convert'>
<h2>Convert</h2>
<div class='key-ui'>
Key: <input id='key' type='textbox' value='1234'><button title='Generate a random key' onclick='generateKey()'>Random</button>
</div>
<button id='encode' title='Encode passwords in an image with a key' onclick='encode()'>Encode to Image</button><br>
<button id='decode' title='Decode passwords from an image with a key' onclick='decode()'>Decode from Image</button><br>
<span id='error' class='hidden'></span>
</div>
<div class='group' id='passwords-encoded'>
<h2>Password-Encoded Image</h2>
<div class='file-io'>
<button onclick='saveImage()' title='Save as a .png file'>Save</button>
<input id='file-image' type='file' onchange='loadImage(this.files[0])'><br>
<span id='error' class='hidden'></span>
</div>
<center><canvas id='input' class='empty' width='100' height='100'></canvas></center>
</div>
<script>
var Canvas = document.querySelector('Canvas');
var Context = Canvas.getContext('2d');
var DecryptionKey = document.getElementById('key');
var PasswordTable = document.querySelector('table#passwords>tbody');
function showError(elem, err) {
if (err) {
elem.innerHTML = err;
elem.className = '';
throw err;
} else {
elem.innerHTML = '';
elem.className = 'hidden';
}
}
function PwdTableError(e) {
return showError(document.querySelector('#passwords-decoded').querySelector('#error'), e);
}
function ConvertError(e) {
return showError(document.querySelector('#convert').querySelector('#error'), e);
}
function ImageError(e) {
return showError(document.querySelector('#passwords-encoded').querySelector('#error'), e);
}
function resetErrors() {
PwdTableError();
ConvertError();
ImageError();
}
Canvas.addEventListener('dragover', function(e) {
e.preventDefault();
});
Canvas.addEventListener('dragenter', function(e) {
Canvas.style.border = '3px dashed blue';
});
Canvas.addEventListener('dragleave', function(e) {
Canvas.style.border = '';
});
Canvas.addEventListener('drop', function(e) {
e.preventDefault();
Canvas.style.border = '';
loadImage(e.dataTransfer.files[0]);
});
function click(element) {
if (document.createEvent) {
var evt = document.createEvent("MouseEvents");
evt.initEvent("click",true,true);
evt.synthetic = true;
element.dispatchEvent(evt,true);
} else if (element.fireEvent) {
var evt = document.createEventObject();
evt.synthetic = true;
element.fireEvent("onclick",evt);
} else {
element.click();
}
}
function loadImage(file) {
if (!file.type.match(/image.*/)) {
return ImageError('Invalid filetype. Must be an image file: *.png, *.jpg.');
} else {
ImageError();
}
let f = new FileReader();
f.onload = function(e) {
let image = new Image();
image.onload = function() {
Context.clearRect(0,0,Canvas.width,Canvas.height);
Canvas.width = image.width;
Canvas.height = image.height;
Context.drawImage(image,0,0,image.width,image.height);
Canvas.className = 'input';
};
image.src = e.target.result;
};
f.readAsDataURL(file);
}
function saveImage() {
var a = document.createElement('a');
a.setAttribute('href', Canvas.toDataURL());
a.setAttribute('download', 'pwds.png');
click(a);
}
function randomRange(a,b) {
return a + ~~((b - a) * Math.random());
}
function generateKey() {
DecryptionKey.value = '';
for (var i = 0, len = randomRange(3,8); i < len; ++i) {
DecryptionKey.value += String.fromCodePoint(randomRange(0x21,0x7E));
}
}
function getKey() {
var dk = DecryptionKey.value;
if (!dk) {
ConvertError('Warning: encryption will be vulnerable without a key.');
} else {
ConvertError();
}
return Array.from(dk).map(function(x,i){return dk.charCodeAt(i)});
}
function clearTable() {
while (PasswordTable.firstChild) PasswordTable.firstChild.remove();
}
function addRecord(data) {
data = data || {};
var row = document.createElement('tr');
var txtWebsite = document.createElement('input');
txtWebsite.setAttribute('type', 'textbox');
txtWebsite.setAttribute('id', 'website');
txtWebsite.setAttribute('placeholder', 'website name or URL');
txtWebsite.setAttribute('value', data.w || '');
var txtUsername = document.createElement('input');
txtUsername.setAttribute('type', 'textbox');
txtUsername.setAttribute('id', 'username');
txtUsername.setAttribute('placeholder', 'username');
txtUsername.setAttribute('value', data.u || '');
var txtPassword = document.createElement('input');
txtPassword.setAttribute('type', 'textbox');
txtPassword.setAttribute('id', 'password');
txtPassword.setAttribute('placeholder', 'password');
txtPassword.setAttribute('value', data.p || '');
var txtEmail = document.createElement('input');
txtEmail.setAttribute('type', 'textbox');
txtEmail.setAttribute('id', 'email');
txtEmail.setAttribute('placeholder', 'email@website.com');
txtEmail.setAttribute('value', data.e || '');
var txtData = document.createElement('input');
txtData.setAttribute('type', 'textbox');
txtData.setAttribute('id', 'data');
txtData.setAttribute('placeholder', 'other data');
txtData.setAttribute('value', data.d || '');
var btnUp = document.createElement('button');
btnUp.setAttribute('id', 'up');
btnUp.addEventListener('click', function () {
var sibling = row.previousSibling;
if (sibling) {
row.remove();
sibling.parentElement.insertBefore(row,sibling);
}
});
btnUp.innerHTML = '▲';
var btnDown = document.createElement('button');
btnDown.setAttribute('id', 'down');
btnDown.addEventListener('click', function () {
var sibling = row.nextSibling;
if (sibling) {
sibling.remove();
row.parentElement.insertBefore(sibling,row);
}
});
btnDown.innerHTML = '▼';
var btnRemove = document.createElement('button');
btnRemove.setAttribute('id', 'remove');
btnRemove.addEventListener('click', function () {row.remove()});
btnRemove.innerHTML = 'X';
var buttonGroup = document.createElement('div');
for (var b of [btnUp,btnDown,btnRemove]) {
buttonGroup.appendChild(b);
}
for (var e of [txtWebsite,txtUsername,txtPassword,txtEmail,txtData,buttonGroup]) {
var td = document.createElement('td');
td.appendChild(e);
row.appendChild(td);
}
PasswordTable.appendChild(row);
}
// https://stackoverflow.com/a/30106551
function b64EncodeUnicode(str) {
return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) {
return String.fromCharCode(parseInt(p1, 16));
}));
}
function b64DecodeUnicode(str) {
return decodeURIComponent(Array.prototype.map.call(atob(str), function(c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
}
function save() {
var data = [];
for (var row of PasswordTable.querySelectorAll('tr')) {
data.push({
w: row.querySelector('input#website').value,
u: row.querySelector('input#username').value,
p: row.querySelector('input#password').value,
e: row.querySelector('input#email').value,
d: row.querySelector('input#data').value
});
}
return b64EncodeUnicode(JSON.stringify(data));
}
function load(data) {
resetErrors();
try {
clearTable();
JSON.parse(data).forEach(addRecord);
} catch (e) {
return PwdTableError(e);
}
}
function saveJson() {
var a = document.createElement('a');
a.setAttribute('href', 'data:application/json;base64,' + save());
a.setAttribute('download', 'pwds.json');
click(a);
}
function loadJson(file) {
resetErrors();
if (file.type.match(/json/)) {
var f = new FileReader();
f.onload = function(e) {
load(e.target.result);
};
f.readAsText(file);
} else {
return PwdTableError('Invalid filetype. Must be a .json file.');
}
}
function decode() {
resetErrors();
if (Canvas.className == 'empty') {
return ImageError('No image to decode.');
}
if (Canvas.width != Canvas.height) {
return ImageError('Image must be a square.');
}
var image = Context.getImageData(0, 0, Canvas.width, Canvas.height),
key = getKey(),
w = image.width,
h = image.height,
x,y,pos,i,k=0,decodedData = '',byte;
read: for (y = 0; y < h; ++y) {
for (x = 0; x < w; ++x) {
pos = 4 * (y * w + x);
for (i = 0; i < 3; ++i, ++k) {
byte = image.data[pos+i];
if (key.length) byte ^= key[k % key.length];
if (byte == 255) break read;
else decodedData += String.fromCodePoint(byte);
}
}
}
try {
decodedData = b64DecodeUnicode(decodedData);
load(decodedData);
} catch (e) {
return ConvertError('Invalid decryption key.');
}
}
function encode() {
var encodedData = save(),
size = Math.ceil(Math.sqrt(encodedData.length/3)),
image = Context.createImageData(size, size),
key = getKey(),
w = image.width,
h = image.height,
x,y,pos,i,k=0,byte;
for (y = 0; y < h; ++y) {
for (x = 0; x < w; ++x) {
pos = 4 * (y * w + x);
image.data[pos+3] = 255;
for (i = 0; i < 3; ++i, ++k) {
byte = (k < encodedData.length) ? encodedData.charCodeAt(k) : 255;
if (key.length) byte ^= key[k % key.length];
image.data[pos+i] = byte;
}
}
}
Canvas.className = 'input';
Canvas.width = Canvas.height = size;
Context.putImageData(image, 0, 0);
}
</script>
</body>
</html>