-
Notifications
You must be signed in to change notification settings - Fork 0
/
aes.js
183 lines (145 loc) · 5.19 KB
/
aes.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
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
const CryptoJS = require('crypto-js')
import { Convert } from "./convert.js";
const { v4: uuidv4 } = require('uuid');
export class Aes {
constructor(){
this.convert = new Convert();
}
reverseString(str) {
if (str === "")
return "";
else
return this.reverseString(str.substr(1)) + str.charAt(0);
}
generatePassphrase(length) {
var result = '';
var characters = 'abcdefghijklmnopqrstuvwxyz0123456789-!#?';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
//combine with uuid
result = uuidv4() + result;
return result;
}
hashSecret(message,newSecret, rounds = 10){
if(rounds < 5000){
rounds = 5000;
}
//rounds = 5000;
let key;
let iv;
if(newSecret == undefined || typeof newSecret == 'undefined' || newSecret.length == 0 ){
newSecret = this.generatePassphrase(128+16);
}
// KEYS FROM newSecret
if(newSecret.length > 128+15){
key = CryptoJS.enc.Utf8.parse(newSecret.slice(0,64)); // Key: Use a WordArray-object instead of a UTF8-string / NodeJS-buffer
iv = CryptoJS.enc.Utf8.parse(newSecret.slice(newSecret.length-64-16,newSecret.length-16));
}
else if(newSecret.length > 91+128+16){
key = CryptoJS.enc.Utf8.parse(newSecret.slice(91,91+64)); // Key: Use a WordArray-object instead of a UTF8-string / NodeJS-buffer
iv = CryptoJS.enc.Utf8.parse(newSecret.slice(newSecret.length-64-16,newSecret.length-16));
}
else{
if(message == undefined || typeof message == 'undefined' || message.length == 0 ){
message = this.generatePassphrase(128+16);
}
let i = 0;
let hashedNewSecret;
let aesSecret = newSecret;
while(i<rounds){
if(i > 0){
aesSecret = hashedNewSecret;
}
hashedNewSecret = CryptoJS.HmacSHA512(String(message),this.reverseString(String(aesSecret))).toString();
// console.log(hashedNewSecret);
i++;
}
key = CryptoJS.enc.Utf8.parse(hashedNewSecret.slice(0,64)); // Key: Use a WordArray-object instead of a UTF8-string / NodeJS-buffer
iv = CryptoJS.enc.Utf8.parse(hashedNewSecret.substr(newSecret.length-64));
}
return { newSecret, key, iv};
}
encrypt(utf8OrObject, whistle = undefined){
let utf8;
if(typeof utf8OrObject == 'object'){
utf8 = JSON.stringify(utf8OrObject);
}
else{
utf8 = utf8OrObject;
}
//string to array buffer
let wordArray = CryptoJS.lib.WordArray.create(this.convert.stringToArrayBuffer(utf8,'utf8'));
return this.encryptWordArray(wordArray,whistle);
}
encryptB64(b64, whistle = undefined){
//string to array buffer
// console.log('about to create word array');
let wordArray = CryptoJS.lib.WordArray.create(this.convert.stringToArrayBuffer(b64,'base64'));
return this.encryptWordArray(wordArray,whistle);
}
encryptUtf8(utf8, whistle = undefined){
//string to array buffer
let wordArray = CryptoJS.lib.WordArray.create(this.convert.stringToArrayBuffer(utf8,'utf8'));
return this.encryptWordArray(wordArray,whistle);
}
encryptWordArray(wordArray, secret = undefined){
// console.log('encryption start...');
let {newSecret, key, iv} = this.hashSecret(secret,secret);
secret = newSecret;
// console.log('aeskey:'+key);
// ENCRYPT
let aesEncryptedB64 = CryptoJS.AES.encrypt(wordArray, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
}).toString();
// RESULT
// console.log(aesEncryptedB64);
// console.log('encryption complete!');
return { secret, aesEncryptedB64 }
}
decryptHex(enc,secret, format = 'utf8'){
let msgB64 = Buffer.from(enc,'hex').toString('base64');
let decr = this.decryptB64(msgB64, secret, format);
// console.log(decr);
try{
if(typeof decr == 'string'){
return JSON.parse(decr)
}
}
catch(e){ return decr; }
}
decryptB64(encryptedQuestFileB64, secret, format = 'utf8'){
let decryptedQuestFileWordArray;
try{
//aes decrypt this file
let {key, iv} = this.hashSecret(secret,secret);
// console.log('iv:'+iv);
decryptedQuestFileWordArray = CryptoJS.AES.decrypt(encryptedQuestFileB64, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
if(decryptedQuestFileWordArray['sigBytes'] < 1){
throw('bad key! tell user!');
}
}
catch(error){
console.log(error);
throw('decryption failed');
}
if(format == 'hex'){
return decryptedQuestFileWordArray.toString(CryptoJS.enc.Hex);
}
else if(format == 'base64'){
return decryptedQuestFileWordArray.toString(CryptoJS.enc.Base64);
}
else if(format == 'utf8'){
let toHex = decryptedQuestFileWordArray.toString(CryptoJS.enc.Hex);
return Buffer.from(toHex,'hex').toString('utf8');
}
// return "123";
}
}