k9crypt 1.0.6 → 1.0.9

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/README.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  This is a special encryption algorithm created for K9Crypt.
4
4
 
5
+ ## Updates
6
+ **v1.0.9**
7
+ - Added new compression algorithm and increased compression ratio.
8
+
5
9
  ## Installation
6
10
 
7
11
  ```bash
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "k9crypt",
3
- "version": "1.0.6",
3
+ "version": "1.0.9",
4
4
  "description": "A special encryption algorithm created for K9Crypt.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -21,10 +21,10 @@
21
21
  "bcrypt": "^5.1.1",
22
22
  "crypto-js": "^4.2.0",
23
23
  "lz4": "^0.6.5",
24
+ "lzma-native": "^8.0.6",
24
25
  "node-forge": "^1.3.1",
25
26
  "xxhash": "^0.3.0"
26
27
  },
27
- "devDependencies": {},
28
28
  "repository": {
29
29
  "type": "git",
30
30
  "url": "git+https://github.com/K9Crypt/module.git"
package/src/index.js CHANGED
@@ -15,10 +15,10 @@ 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 { iv1, iv2, encrypted, tag1 } = encrypt(compressed, key);
19
- const dataToHash = Buffer.concat([salt, iv1, iv2, encrypted, tag1]);
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, iv1, iv2, encrypted, tag1, dataHash]);
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
24
  console.log('Encryption failed');
@@ -31,7 +31,10 @@ class K9crypt {
31
31
  const salt = data.slice(0, SALT_SIZE);
32
32
  const iv1 = data.slice(SALT_SIZE, SALT_SIZE + IV_SIZE);
33
33
  const iv2 = data.slice(SALT_SIZE + IV_SIZE, SALT_SIZE + 2 * IV_SIZE);
34
- const encrypted = data.slice(SALT_SIZE + 2 * IV_SIZE, -TAG_SIZE - 64);
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);
35
38
  const tag1 = data.slice(-TAG_SIZE - 64, -64);
36
39
  const dataHash = data.slice(-64);
37
40
 
@@ -41,7 +44,7 @@ class K9crypt {
41
44
  }
42
45
 
43
46
  const key = await deriveKey(this.secretKey, salt);
44
- const decrypted = decrypt(encrypted, key, iv1, iv2, tag1);
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) {
@@ -1,19 +1,44 @@
1
1
  const zlib = require('zlib');
2
+ const lzma = require('lzma-native');
2
3
 
3
- exports.compress = (data) => {
4
- return new Promise((resolve, reject) => {
5
- zlib.brotliCompress(Buffer.from(data, 'utf8'), (err, compressed) => {
6
- if (err) reject(err);
7
- else resolve(compressed);
4
+ exports.compress = async (data) => {
5
+ try {
6
+ const brotliParams = {
7
+ params: {
8
+ [zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
9
+ [zlib.constants.BROTLI_PARAM_QUALITY]: zlib.constants.BROTLI_MAX_QUALITY,
10
+ [zlib.constants.BROTLI_PARAM_SIZE_HINT]: Buffer.byteLength(data, 'utf8'),
11
+ [zlib.constants.BROTLI_PARAM_LGWIN]: 24
12
+ }
13
+ };
14
+
15
+ const brotliCompressed = await new Promise((resolve, reject) => {
16
+ zlib.brotliCompress(Buffer.from(data, 'utf8'), brotliParams, (err, compressed) => {
17
+ if (err) reject(err);
18
+ else resolve(compressed);
19
+ });
8
20
  });
9
- });
21
+
22
+ const lzmaCompressed = await lzma.compress(brotliCompressed, 9);
23
+ return lzmaCompressed;
24
+ } catch (error) {
25
+ throw new Error(`Compression error: ${error.message}`);
26
+ }
10
27
  };
11
28
 
12
- exports.decompress = (data) => {
13
- return new Promise((resolve, reject) => {
14
- zlib.brotliDecompress(data, (err, decompressed) => {
15
- if (err) reject(err);
16
- else resolve(decompressed);
29
+ exports.decompress = async (data) => {
30
+ try {
31
+ const lzmaDecompressed = await lzma.decompress(data);
32
+
33
+ const brotliDecompressed = await new Promise((resolve, reject) => {
34
+ zlib.brotliDecompress(lzmaDecompressed, (err, decompressed) => {
35
+ if (err) reject(err);
36
+ else resolve(decompressed);
37
+ });
17
38
  });
18
- });
19
- };
39
+
40
+ return brotliDecompressed;
41
+ } catch (error) {
42
+ throw new Error(`Decompression error: ${error.message}`);
43
+ }
44
+ };
@@ -12,15 +12,36 @@ exports.encrypt = (data, key) => {
12
12
  const cipher2 = crypto.createCipheriv('aes-256-cbc', key, iv2);
13
13
  let encrypted2 = cipher2.update(encrypted1);
14
14
  encrypted2 = Buffer.concat([encrypted2, cipher2.final()]);
15
- const permutedEncrypted = reverseBuffer(encrypted2);
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);
16
28
 
17
- return { iv1, iv2, encrypted: permutedEncrypted, tag1 };
29
+ return { iv1, iv2, iv3, iv4, iv5, encrypted: permutedEncrypted, tag1 };
18
30
  };
19
31
 
20
- exports.decrypt = (encrypted, key, iv1, iv2, tag1) => {
32
+ exports.decrypt = (encrypted, key, iv1, iv2, iv3, iv4, iv5, tag1) => {
21
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()]);
22
43
  const decipher2 = crypto.createDecipheriv('aes-256-cbc', key, iv2);
23
- let decrypted2 = decipher2.update(originalEncrypted);
44
+ let decrypted2 = decipher2.update(decrypted3);
24
45
  decrypted2 = Buffer.concat([decrypted2, decipher2.final()]);
25
46
  const decipher1 = crypto.createDecipheriv('aes-256-gcm', key, iv1);
26
47
  decipher1.setAuthTag(tag1);
@@ -28,4 +49,4 @@ exports.decrypt = (encrypted, key, iv1, iv2, tag1) => {
28
49
  decrypted1 = Buffer.concat([decrypted1, decipher1.final()]);
29
50
 
30
51
  return decrypted1;
31
- };
52
+ };