k9crypt 1.0.5 → 1.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +13 -1
- package/package.json +1 -1
- package/src/index.js +14 -12
- package/src/utils/encryption.js +46 -13
- package/src/utils/hashing.js +6 -3
- package/src/utils/keyDerivation.js +6 -2
- package/src/utils/math.js +15 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 K9Crypt
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -2,6 +2,18 @@
|
|
|
2
2
|
|
|
3
3
|
This is a special encryption algorithm created for K9Crypt.
|
|
4
4
|
|
|
5
|
+
## Updates
|
|
6
|
+
**v1.0.8**
|
|
7
|
+
- Enhanced encryption security with 5-layer encryption system
|
|
8
|
+
- Added multiple AES encryption modes in sequence:
|
|
9
|
+
- AES-256-GCM
|
|
10
|
+
- AES-256-CBC
|
|
11
|
+
- AES-256-CFB
|
|
12
|
+
- AES-256-OFB
|
|
13
|
+
- AES-256-CTR
|
|
14
|
+
- Each layer now uses its own initialization vector (IV)
|
|
15
|
+
- Improved data integrity with comprehensive authentication
|
|
16
|
+
|
|
5
17
|
## Installation
|
|
6
18
|
|
|
7
19
|
```bash
|
|
@@ -33,4 +45,4 @@ test();
|
|
|
33
45
|
```
|
|
34
46
|
|
|
35
47
|
## License
|
|
36
|
-
This project is licensed under the MIT license.
|
|
48
|
+
This project is licensed under the MIT license.
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -15,14 +15,13 @@ class K9crypt {
|
|
|
15
15
|
const compressed = await compress(plaintext);
|
|
16
16
|
const salt = crypto.randomBytes(SALT_SIZE);
|
|
17
17
|
const key = await deriveKey(this.secretKey, salt);
|
|
18
|
-
const {
|
|
19
|
-
const dataToHash = Buffer.concat([salt,
|
|
18
|
+
const { iv1, iv2, iv3, iv4, iv5, encrypted, tag1 } = encrypt(compressed, key);
|
|
19
|
+
const dataToHash = Buffer.concat([salt, iv1, iv2, iv3, iv4, iv5, encrypted, tag1]);
|
|
20
20
|
const dataHash = hash(dataToHash);
|
|
21
|
-
const result = Buffer.concat([salt,
|
|
21
|
+
const result = Buffer.concat([salt, iv1, iv2, iv3, iv4, iv5, encrypted, tag1, dataHash]);
|
|
22
22
|
return result.toString('base64');
|
|
23
23
|
} catch (error) {
|
|
24
|
-
console.
|
|
25
|
-
throw new Error('Encryption failed');
|
|
24
|
+
console.log('Encryption failed');
|
|
26
25
|
}
|
|
27
26
|
}
|
|
28
27
|
|
|
@@ -30,23 +29,26 @@ class K9crypt {
|
|
|
30
29
|
try {
|
|
31
30
|
const data = Buffer.from(ciphertext, 'base64');
|
|
32
31
|
const salt = data.slice(0, SALT_SIZE);
|
|
33
|
-
const
|
|
34
|
-
const
|
|
35
|
-
const
|
|
32
|
+
const iv1 = data.slice(SALT_SIZE, SALT_SIZE + IV_SIZE);
|
|
33
|
+
const iv2 = data.slice(SALT_SIZE + IV_SIZE, SALT_SIZE + 2 * IV_SIZE);
|
|
34
|
+
const iv3 = data.slice(SALT_SIZE + 2 * IV_SIZE, SALT_SIZE + 3 * IV_SIZE);
|
|
35
|
+
const iv4 = data.slice(SALT_SIZE + 3 * IV_SIZE, SALT_SIZE + 4 * IV_SIZE);
|
|
36
|
+
const iv5 = data.slice(SALT_SIZE + 4 * IV_SIZE, SALT_SIZE + 5 * IV_SIZE);
|
|
37
|
+
const encrypted = data.slice(SALT_SIZE + 5 * IV_SIZE, -TAG_SIZE - 64);
|
|
38
|
+
const tag1 = data.slice(-TAG_SIZE - 64, -64);
|
|
36
39
|
const dataHash = data.slice(-64);
|
|
37
40
|
|
|
38
41
|
const dataToVerify = data.slice(0, -64);
|
|
39
42
|
if (!verifyHash(dataToVerify, dataHash)) {
|
|
40
|
-
|
|
43
|
+
console.log('Data integrity check failed');
|
|
41
44
|
}
|
|
42
45
|
|
|
43
46
|
const key = await deriveKey(this.secretKey, salt);
|
|
44
|
-
const decrypted = decrypt(encrypted, key,
|
|
47
|
+
const decrypted = decrypt(encrypted, key, iv1, iv2, iv3, iv4, iv5, tag1);
|
|
45
48
|
const decompressed = await decompress(decrypted);
|
|
46
49
|
return decompressed.toString('utf8');
|
|
47
50
|
} catch (error) {
|
|
48
|
-
console.
|
|
49
|
-
throw new Error('Decryption failed');
|
|
51
|
+
console.log('Decryption failed');
|
|
50
52
|
}
|
|
51
53
|
}
|
|
52
54
|
}
|
package/src/utils/encryption.js
CHANGED
|
@@ -1,19 +1,52 @@
|
|
|
1
1
|
const crypto = require('crypto');
|
|
2
2
|
const { IV_SIZE } = require('../constants');
|
|
3
|
+
const { reverseBuffer } = require('./math');
|
|
3
4
|
|
|
4
5
|
exports.encrypt = (data, key) => {
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
let
|
|
8
|
-
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
6
|
+
const iv1 = crypto.randomBytes(IV_SIZE);
|
|
7
|
+
const cipher1 = crypto.createCipheriv('aes-256-gcm', key, iv1);
|
|
8
|
+
let encrypted1 = cipher1.update(data);
|
|
9
|
+
encrypted1 = Buffer.concat([encrypted1, cipher1.final()]);
|
|
10
|
+
const tag1 = cipher1.getAuthTag();
|
|
11
|
+
const iv2 = crypto.randomBytes(IV_SIZE);
|
|
12
|
+
const cipher2 = crypto.createCipheriv('aes-256-cbc', key, iv2);
|
|
13
|
+
let encrypted2 = cipher2.update(encrypted1);
|
|
14
|
+
encrypted2 = Buffer.concat([encrypted2, cipher2.final()]);
|
|
15
|
+
const iv3 = crypto.randomBytes(IV_SIZE);
|
|
16
|
+
const cipher3 = crypto.createCipheriv('aes-256-cfb', key, iv3);
|
|
17
|
+
let encrypted3 = cipher3.update(encrypted2);
|
|
18
|
+
encrypted3 = Buffer.concat([encrypted3, cipher3.final()]);
|
|
19
|
+
const iv4 = crypto.randomBytes(IV_SIZE);
|
|
20
|
+
const cipher4 = crypto.createCipheriv('aes-256-ofb', key, iv4);
|
|
21
|
+
let encrypted4 = cipher4.update(encrypted3);
|
|
22
|
+
encrypted4 = Buffer.concat([encrypted4, cipher4.final()]);
|
|
23
|
+
const iv5 = crypto.randomBytes(IV_SIZE);
|
|
24
|
+
const cipher5 = crypto.createCipheriv('aes-256-ctr', key, iv5);
|
|
25
|
+
let encrypted5 = cipher5.update(encrypted4);
|
|
26
|
+
encrypted5 = Buffer.concat([encrypted5, cipher5.final()]);
|
|
27
|
+
const permutedEncrypted = reverseBuffer(encrypted5);
|
|
12
28
|
|
|
13
|
-
|
|
14
|
-
const decipher = crypto.createDecipheriv('aes-256-gcm', key, iv);
|
|
15
|
-
decipher.setAuthTag(tag);
|
|
16
|
-
let decrypted = decipher.update(encrypted);
|
|
17
|
-
decrypted = Buffer.concat([decrypted, decipher.final()]);
|
|
18
|
-
return decrypted;
|
|
29
|
+
return { iv1, iv2, iv3, iv4, iv5, encrypted: permutedEncrypted, tag1 };
|
|
19
30
|
};
|
|
31
|
+
|
|
32
|
+
exports.decrypt = (encrypted, key, iv1, iv2, iv3, iv4, iv5, tag1) => {
|
|
33
|
+
const originalEncrypted = reverseBuffer(encrypted, true);
|
|
34
|
+
const decipher5 = crypto.createDecipheriv('aes-256-ctr', key, iv5);
|
|
35
|
+
let decrypted5 = decipher5.update(originalEncrypted);
|
|
36
|
+
decrypted5 = Buffer.concat([decrypted5, decipher5.final()]);
|
|
37
|
+
const decipher4 = crypto.createDecipheriv('aes-256-ofb', key, iv4);
|
|
38
|
+
let decrypted4 = decipher4.update(decrypted5);
|
|
39
|
+
decrypted4 = Buffer.concat([decrypted4, decipher4.final()]);
|
|
40
|
+
const decipher3 = crypto.createDecipheriv('aes-256-cfb', key, iv3);
|
|
41
|
+
let decrypted3 = decipher3.update(decrypted4);
|
|
42
|
+
decrypted3 = Buffer.concat([decrypted3, decipher3.final()]);
|
|
43
|
+
const decipher2 = crypto.createDecipheriv('aes-256-cbc', key, iv2);
|
|
44
|
+
let decrypted2 = decipher2.update(decrypted3);
|
|
45
|
+
decrypted2 = Buffer.concat([decrypted2, decipher2.final()]);
|
|
46
|
+
const decipher1 = crypto.createDecipheriv('aes-256-gcm', key, iv1);
|
|
47
|
+
decipher1.setAuthTag(tag1);
|
|
48
|
+
let decrypted1 = decipher1.update(decrypted2);
|
|
49
|
+
decrypted1 = Buffer.concat([decrypted1, decipher1.final()]);
|
|
50
|
+
|
|
51
|
+
return decrypted1;
|
|
52
|
+
};
|
package/src/utils/hashing.js
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
const crypto = require('crypto');
|
|
2
|
-
const {
|
|
2
|
+
const { HMAC_KEY } = require('../constants');
|
|
3
|
+
const { reverseHash } = require('./math');
|
|
3
4
|
|
|
4
5
|
exports.hash = (data) => {
|
|
5
6
|
const hmac = crypto.createHmac('sha512', HMAC_KEY);
|
|
6
7
|
hmac.update(data);
|
|
7
|
-
|
|
8
|
+
const digest = hmac.digest();
|
|
9
|
+
|
|
10
|
+
return reverseHash(digest);
|
|
8
11
|
};
|
|
9
12
|
|
|
10
|
-
exports.verifyHash = (data, hash) => crypto.timingSafeEqual(exports.hash(data), hash);
|
|
13
|
+
exports.verifyHash = (data, hash) => crypto.timingSafeEqual(exports.hash(data), hash);
|
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
const crypto = require('crypto');
|
|
2
2
|
const { PBKDF2_ITERATIONS, KEY_SIZE, PEPPER } = require('../constants');
|
|
3
|
+
const { enhanceKey } = require('./math');
|
|
3
4
|
|
|
4
5
|
exports.deriveKey = (password, salt) => {
|
|
5
6
|
return new Promise((resolve, reject) => {
|
|
6
7
|
const pepperedPassword = password + PEPPER;
|
|
7
8
|
crypto.pbkdf2(pepperedPassword, salt, PBKDF2_ITERATIONS, KEY_SIZE, 'sha512', (err, key) => {
|
|
8
9
|
if (err) reject(err);
|
|
9
|
-
|
|
10
|
+
|
|
11
|
+
const enhancedKey = enhanceKey(key);
|
|
12
|
+
|
|
13
|
+
resolve(enhancedKey);
|
|
10
14
|
});
|
|
11
15
|
});
|
|
12
|
-
};
|
|
16
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
exports.reverseBuffer = (data, reverse = false) => {
|
|
2
|
+
if (reverse) {
|
|
3
|
+
return Buffer.from(data.toString('hex').split('').reverse().join(''), 'hex');
|
|
4
|
+
}
|
|
5
|
+
return Buffer.from(data.toString('hex').split('').reverse().join(''), 'hex');
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
exports.reverseHash = (hash) => {
|
|
9
|
+
return Buffer.from(hash.toString('hex').split('').reverse().join(''), 'hex');
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
exports.enhanceKey = (key) => {
|
|
13
|
+
return Buffer.from(key.toString('hex').split('').reverse().join(''), 'hex');
|
|
14
|
+
};
|
|
15
|
+
|