-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpasswd.html
169 lines (163 loc) · 5.89 KB
/
passwd.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
<!DOCTYPE html>
<html>
<head>
<title>Encrypt/Decrypt Password</title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="js/asmcrypto.js"></script>
<script>
// asmCrypto.SHA512.base64(base64Combo);
function randomBytes(length) {
if (length <= 0) {
throw 'length=[' + length + '] must >= 0';
}
var ix;
var num;
var bytes = new Uint8Array(length);
for (ix = 0; ix < length; ix += 1) {
num = Math.floor((Math.random() * 256));
bytes[ix] = num;
}
return bytes;
}
function aesEncrypt(cipher, data, skey, padding, ivec) {
if (cipher == 'AES-CBC') {
return asmCrypto.AES_CBC.encrypt(data, skey, padding, ivec);
} else if (cipher == 'AES-CFB') {
return asmCrypto.AES_CFB.encrypt(data, skey, ivec);
} else if (cipher == 'AES-OFB') {
return asmCrypto.AES_OFB.encrypt(data, skey, ivec);
} else if (cipher == 'AES-CTR') {
return asmCrypto.AES_CTR.encrypt(data, skey, ivec);
} else {
return asmCrypto.AES_ECB.encrypt(data, skey, padding);
}
}
function aesDecrypt(cipher, encrypted, skey, padding, ivec) {
if (cipher == 'AES-CBC') {
return asmCrypto.AES_CBC.decrypt(encrypted, skey, padding, ivec);
} else if (cipher == 'AES-CFB') {
return asmCrypto.AES_CFB.decrypt(encrypted, skey, ivec);
} else if (cipher == 'AES-OFB') {
return asmCrypto.AES_OFB.decrypt(encrypted, skey, ivec);
} else if (cipher == 'AES-CTR') {
return asmCrypto.AES_CTR.decrypt(encrypted, skey, ivec);
} else {
return asmCrypto.AES_ECB.decrypt(encrypted, skey, padding);
}
}
function encrypt(cipher, string) {
/*
* input string plain text
* return Base64(skey + ivec + encrypted)
*/
var skey = randomBytes(16);
var ivec = randomBytes(16);
var padding = true;
var data = asmCrypto.string_to_bytes(string);
var encrypted = aesEncrypt(cipher, data, skey, padding, ivec);
var encryptedLen = encrypted.length;
var combo = new Uint8Array(16 + 16 + encryptedLen);
var ix;
for (ix = 0; ix < 16; ix += 1) {
combo[ix] = skey[ix];
}
for (ix = 0; ix < 16; ix += 1) {
combo[16 + ix] = ivec[ix];
}
for (ix = 0; ix < encryptedLen; ix += 1) {
combo[32 + ix] = encrypted[ix];
}
return asmCrypto.bytes_to_base64(combo);
}
function decrypt(cipher, base64Combo) {
/*
* input Base64(skey + ivec + encrypted)
* output string decrypted text
*/
var combo = asmCrypto.base64_to_bytes(base64Combo);
var skey = new Uint8Array(16);
var ivec = new Uint8Array(16);
var encryptedLen = combo.length - 16 - 16;
var encrypted = new Uint8Array(encryptedLen);
var padding = true;
var ix;
for (ix = 0; ix < 16; ix += 1) {
skey[ix] = combo[ix];
}
for (ix = 0; ix < 16; ix += 1) {
ivec[ix] = combo[16 + ix];
}
for (ix = 0; ix < encryptedLen; ix += 1) {
encrypted[ix] = combo[32 + ix];
}
var decrypted = aesDecrypt(cipher, encrypted, skey, padding, ivec);
return asmCrypto.bytes_to_string(decrypted);
}
function encryptText() {
var elText = document.getElementById('txtText');
var elCipher = document.getElementById('cboCipher');
var elStatus = document.getElementById('spnStatus');
try {
var cipherVal = elCipher.value;
var textVal = elText.value;
var encrypted = encrypt(cipherVal, textVal);
elText.value = '(' + cipherVal + ')' + encrypted;
elStatus.innerHTML = '[' + cipherVal + '] Success encrypted.';
} catch (err) {
elStatus.innerHTML = '[' + cipherVal + '] Fail encrypted.\n' + err;
}
}
function decryptText() {
var elText = document.getElementById('txtText');
var elCipher = document.getElementById('cboCipher');
var elStatus = document.getElementById('spnStatus');
try {
var cipherVal = elCipher.value;
var textVal = elText.value;
var pos1 = textVal.indexOf('(', 0);
var pos2 = textVal.indexOf(')', 0);
if (pos1 >= 0 && pos2 > pos1) {
var cipherDetected = textVal.substring(pos1 + 1, pos2);
if (cipherDetected == 'AES-ECB'
|| cipherDetected == 'AES-CBC'
|| cipherDetected == 'AES-CFB'
|| cipherDetected == 'AES-OFB'
|| cipherDetected == 'AES-CTR') {
cipherVal = cipherDetected;
}
textVal = textVal.substring(pos2 + 1);
}
var decrypted = decrypt(cipherVal, textVal);
elText.value = decrypted;
elStatus.innerHTML = '[' + cipherVal + '] Success decrypted.';
} catch (err) {
elStatus.innerHTML = '[' + cipherVal + '] Fail decrypted.\n' + err;
}
}
</script>
</head>
<body>
<div>Using <a href="https://github.com/vibornoff/asmcrypto.js" target="asmcrypto">asmCrypto</a></div>
<h3>Only ASCII text, no wide character, no chinese.</h3>
<select id="cboCipher">
<option value="AES-ECB">AES ECB</option>
<option value="AES-CBC" selected="selected">AES CBC</option>
<option value="AES-CFB">AES CFB</option>
<option value="AES-OFB">AES OFB</option>
<option value="AES-CTR">AES CTR</option>
</select>
<input type="button" onclick="encryptText();" value="Encrypt" />
<input type="button" onclick="decryptText();" value="Decrypt" />
<span id="spnStatus"></span>
<br />
<textarea id="txtText" name="msg" cols="80" rows="25">supercalifragilisticexpialidocious
dociousaliexpilisticfragicalisuper
Lopado-temacho-selacho-galeo-kranio-leipsano-drim-hypo-trimmato-silphio-parao-melito-katakechy-meno-kichl-epi-kossypho-phatto-perister-alektryon-opte-kephallio-kigklo-peleio-lagoio-siraio-baphe-tragano-pterygon
Pneumonoultramicroscopicsilicovolcanoconiosis
Pseudopseudohypoparathyroidism
Floccinaucinihilipilification
Antidisestablishmentarianism
</textarea>
</body>
</html>