-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdecrypt.js
50 lines (35 loc) · 1.25 KB
/
decrypt.js
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
function protect_key(password, salt){
// 1000 iterations takes a couple seconds in the browser. Wouldn't want to go much higher if this is a browser implementation
var iterations = 10000;
// make your own hash if you don't know this one
return CryptoJS.PBKDF2(password, salt, { keySize: 512/32, iterations: iterations }).toString();
}
var text_fields = ['value', 'textContent'];
var text_field = -1;
for(let field in text_fields){
if(document.activeElement[text_fields[field]]) {
text_field = field;
break;
}
}
if(text_field < 0){
alert("Error, no value found.");
}
var original_value = document.activeElement[text_fields[text_field]];
if(original_value.length <= 0){
alert("Error, empty value.");
}
var password = prompt("Set password");
var salt = original_value.substring(0,32);
original_value = original_value.substring(32);
var key = protect_key(password, salt);
var bytes = CryptoJS.AES.decrypt(original_value, key);
var plaintext = bytes.toString(CryptoJS.enc.Utf8);
var orig_hash = plaintext.substring(0, 64);
var result = plaintext.substring(64);
var result_hash = CryptoJS.SHA256(result).toString();
if(orig_hash == result_hash){
document.activeElement[text_fields[text_field]] = result;
} else {
alert("Error, bad decryption.");
}