Przeglądaj źródła

Implement IGE encryption and decryption

implemented without using any other C modules. Inspired by the telethon module.
painor 5 lat temu
rodzic
commit
0450710cca
1 zmienionych plików z 72 dodań i 26 usunięć
  1. 72 26
      crypto/AES.js

+ 72 - 26
crypto/AES.js

@@ -1,27 +1,73 @@
+const TextEncoder = require("util").TextEncoder;
+const TextDecoder = require("util").TextDecoder;
+const aesjs = require('aes-js');
+
+
 class AES {
- decryptIge(){/*
-     var aes, blocks_count, cipher_text_block, iv1, iv2, plain_text, plain_text_block;
-     iv1 = iv.slice(0, (Math.floor(iv.length / 2)));
-     iv2 = iv.slice((Math.floor(iv.length / 2)));
-     aes = new pyaes.AES(key);
-     plain_text = ([0] * cipher_text.length);
-     blocks_count = (Math.floor(cipher_text.length / 16));
-     cipher_text_block = ([0] * 16);
-     for (var block_index = 0, _pj_a = blocks_count; (block_index < _pj_a); block_index += 1) {
-         for (var i = 0, _pj_b = 16; (i < _pj_b); i += 1) {
-             cipher_text_block[i] = (cipher_text[((block_index * 16) + i)] ^ iv2[i]);
-         }
-         plain_text_block = aes.decrypt(cipher_text_block);
-         for (var i = 0, _pj_b = 16; (i < _pj_b); i += 1) {
-             plain_text_block[i] ^= iv1[i];
-         }
-         iv1 = cipher_text.slice((block_index * 16), ((block_index * 16) + 16));
-         iv2 = plain_text_block.slice(0, 16);
-         plain_text.slice((block_index * 16), ((block_index * 16) + 16)) = plain_text_block.slice(0, 16);
-     }
-     return bytes(plain_text);
-*/
-
-
- }
-}
+    static decryptIge(cipherText, key, iv) {
+        let iv1 = iv.slice(0, Math.floor(iv.length / 2));
+        let iv2 = iv.slice(Math.floor(iv.length / 2));
+        let plainText = new Array(cipherText.length).fill(0);
+        let aes = new aesjs.AES(key);
+        let blocksCount = Math.floor(plainText.length / 16);
+        let cipherTextBlock = new Array(16).fill(0);
+
+        for (let blockIndex = 0; blockIndex < blocksCount; blockIndex++) {
+            for (let i = 0; i < 16; i++) {
+                cipherTextBlock[i] = cipherText[blockIndex * 16 + i] ^ iv2[i];
+            }
+            let plainTextBlock = aes.decrypt(cipherTextBlock);
+            for (let i = 0; i < 16; i++) {
+                plainTextBlock[i] ^= iv1[i];
+            }
+
+            iv1 = cipherText.slice(blockIndex * 16, blockIndex * 16 + 16);
+            iv2 = plainTextBlock.slice(0, 16);
+            plainText = new Uint8Array([
+                ...plainText.slice(0, blockIndex * 16),
+                ...plainTextBlock.slice(0, 16),
+                ...plainText.slice(blockIndex * 16 + 16)
+            ]);
+
+        }
+        return plainText;
+    }
+
+    static encryptIge(plainText, key, iv) {
+        if (plainText.length % 16 !== 0) {
+            let padding = new Uint8Array(16 - plainText.length % 16);
+            plainText = new Uint8Array([
+                ...plainText,
+                ...padding,
+            ]);
+        }
+        let iv1 = iv.slice(0, Math.floor(iv.length / 2));
+        let iv2 = iv.slice(Math.floor(iv.length / 2));
+        let aes = new aesjs.AES(key);
+        let blocksCount = Math.floor(plainText.length / 16);
+        let cipherText = new Array(plainText.length).fill(0);
+        for (let blockIndex = 0; blockIndex < blocksCount; blockIndex++) {
+            let plainTextBlock = plainText.slice(blockIndex * 16, blockIndex * 16 + 16);
+
+            for (let i = 0; i < 16; i++) {
+                plainTextBlock[i] ^= iv1[i];
+            }
+            let cipherTextBlock = aes.encrypt(plainTextBlock);
+            for (let i = 0; i < 16; i++) {
+                cipherTextBlock[i] ^= iv2[i];
+            }
+
+            iv1 = cipherTextBlock.slice(0, 16);
+            iv2 = plainText.slice(blockIndex * 16, blockIndex * 16 + 16);
+            cipherText = new Uint8Array([
+                ...cipherText.slice(0, blockIndex * 16),
+                ...cipherTextBlock.slice(0, 16),
+                ...cipherText.slice(blockIndex * 16 + 16)
+            ]);
+        }
+        return cipherText;
+    }
+
+}
+
+exports.AES = AES;