Browse Source

Removed CryptoJS encodings from Blowfish operations.

n1474335 7 years ago
parent
commit
68e52d1645
2 changed files with 95 additions and 40 deletions
  1. 42 20
      src/core/config/OperationConfig.js
  2. 53 20
      src/core/operations/Cipher.js

+ 42 - 20
src/core/config/OperationConfig.js

@@ -1126,12 +1126,12 @@ const OperationConfig = {
             {
                 name: "Input",
                 type: "option",
-                value: Cipher.IO_FORMAT3
+                value: Cipher.IO_FORMAT4
             },
             {
                 name: "Output",
                 type: "option",
-                value: Cipher.IO_FORMAT2
+                value: Cipher.IO_FORMAT3
             },
             {
                 name: "GCM Tag",
@@ -1167,12 +1167,12 @@ const OperationConfig = {
             {
                 name: "Input",
                 type: "option",
-                value: Cipher.IO_FORMAT2
+                value: Cipher.IO_FORMAT3
             },
             {
                 name: "Output",
                 type: "option",
-                value: Cipher.IO_FORMAT3
+                value: Cipher.IO_FORMAT4
             },
         ]
     },
@@ -1202,12 +1202,12 @@ const OperationConfig = {
             {
                 name: "Input",
                 type: "option",
-                value: Cipher.IO_FORMAT3
+                value: Cipher.IO_FORMAT4
             },
             {
                 name: "Output",
                 type: "option",
-                value: Cipher.IO_FORMAT2
+                value: Cipher.IO_FORMAT3
             },
         ]
     },
@@ -1237,12 +1237,12 @@ const OperationConfig = {
             {
                 name: "Input",
                 type: "option",
-                value: Cipher.IO_FORMAT2
+                value: Cipher.IO_FORMAT3
             },
             {
                 name: "Output",
                 type: "option",
-                value: Cipher.IO_FORMAT3
+                value: Cipher.IO_FORMAT4
             },
         ]
     },
@@ -1272,12 +1272,12 @@ const OperationConfig = {
             {
                 name: "Input",
                 type: "option",
-                value: Cipher.IO_FORMAT3
+                value: Cipher.IO_FORMAT4
             },
             {
                 name: "Output",
                 type: "option",
-                value: Cipher.IO_FORMAT2
+                value: Cipher.IO_FORMAT3
             },
         ]
     },
@@ -1307,18 +1307,18 @@ const OperationConfig = {
             {
                 name: "Input",
                 type: "option",
-                value: Cipher.IO_FORMAT2
+                value: Cipher.IO_FORMAT3
             },
             {
                 name: "Output",
                 type: "option",
-                value: Cipher.IO_FORMAT3
+                value: Cipher.IO_FORMAT4
             },
         ]
     },
     "Blowfish Decrypt": {
         module: "Ciphers",
-        description: "Blowfish is a symmetric-key block cipher designed in 1993 by Bruce Schneier and included in a large number of cipher suites and encryption products. AES now receives more attention.",
+        description: "Blowfish is a symmetric-key block cipher designed in 1993 by Bruce Schneier and included in a large number of cipher suites and encryption products. AES now receives more attention.<br><br><b>IV:</b> The Initialization Vector should be 8 bytes long. If not entered, it will default to 8 null bytes.",
         inputType: "string",
         outputType: "string",
         args: [
@@ -1326,7 +1326,13 @@ const OperationConfig = {
                 name: "Key",
                 type: "toggleString",
                 value: "",
-                toggleValues: Cipher.CJS_IO_FORMAT
+                toggleValues: Cipher.IO_FORMAT1
+            },
+            {
+                name: "IV",
+                type: "toggleString",
+                value: "",
+                toggleValues: Cipher.IO_FORMAT1
             },
             {
                 name: "Mode",
@@ -1334,15 +1340,20 @@ const OperationConfig = {
                 value: Cipher.BLOWFISH_MODES
             },
             {
-                name: "Input format",
+                name: "Input",
                 type: "option",
-                value: Cipher.IO_FORMAT1
+                value: Cipher.BLOWFISH_OUTPUT_TYPES
+            },
+            {
+                name: "Output",
+                type: "option",
+                value: Cipher.IO_FORMAT3
             },
         ]
     },
     "Blowfish Encrypt": {
         module: "Ciphers",
-        description: "Blowfish is a symmetric-key block cipher designed in 1993 by Bruce Schneier and included in a large number of cipher suites and encryption products. AES now receives more attention.",
+        description: "Blowfish is a symmetric-key block cipher designed in 1993 by Bruce Schneier and included in a large number of cipher suites and encryption products. AES now receives more attention.<br><br><b>IV:</b> The Initialization Vector should be 8 bytes long. If not entered, it will default to 8 null bytes.",
         inputType: "string",
         outputType: "string",
         args: [
@@ -1350,7 +1361,13 @@ const OperationConfig = {
                 name: "Key",
                 type: "toggleString",
                 value: "",
-                toggleValues: Cipher.CJS_IO_FORMAT
+                toggleValues: Cipher.IO_FORMAT1
+            },
+            {
+                name: "IV",
+                type: "toggleString",
+                value: "",
+                toggleValues: Cipher.IO_FORMAT1
             },
             {
                 name: "Mode",
@@ -1358,9 +1375,14 @@ const OperationConfig = {
                 value: Cipher.BLOWFISH_MODES
             },
             {
-                name: "Output format",
+                name: "Input",
                 type: "option",
-                value: Cipher.IO_FORMAT1
+                value: Cipher.IO_FORMAT3
+            },
+            {
+                name: "Output",
+                type: "option",
+                value: Cipher.BLOWFISH_OUTPUT_TYPES
             },
         ]
     },

+ 53 - 20
src/core/operations/Cipher.js

@@ -24,12 +24,17 @@ const Cipher = {
     * @constant
     * @default
     */
-    IO_FORMAT2: ["Raw", "Hex"],
+    IO_FORMAT2: ["UTF8", "Latin1", "Hex", "Base64"],
     /**
     * @constant
     * @default
     */
-    IO_FORMAT3: ["Hex", "Raw"],
+    IO_FORMAT3: ["Raw", "Hex"],
+    /**
+    * @constant
+    * @default
+    */
+    IO_FORMAT4: ["Hex", "Raw"],
     /**
      * @constant
      * @default
@@ -270,12 +275,29 @@ DES uses a key length of 8 bytes (64 bits).`;
      * @constant
      * @default
      */
-    BLOWFISH_MODES: ["ECB", "CBC", "PCBC", "CFB", "OFB", "CTR"],
+    BLOWFISH_MODES: ["CBC", "PCBC", "CFB", "OFB", "CTR", "ECB"],
     /**
      * @constant
      * @default
      */
-    BLOWFISH_OUTPUT_TYPES: ["Base64", "Hex", "String", "Raw"],
+    BLOWFISH_OUTPUT_TYPES: ["Hex", "Base64", "Raw"],
+
+    /**
+     * Lookup table for Blowfish output types.
+     * 
+     * @private
+     */
+    _BLOWFISH_OUTPUT_TYPE_LOOKUP: {
+        Base64: 0, Hex: 1, String: 2, Raw: 3
+    },
+    /**
+     * Lookup table for Blowfish modes.
+     * 
+     * @private
+     */
+    _BLOWFISH_MODE_LOOKUP: {
+        ECB: 0, CBC: 1, PCBC: 2, CFB: 3, OFB: 4, CTR: 5
+    },
 
     /**
      * Blowfish Encrypt operation.
@@ -285,19 +307,24 @@ DES uses a key length of 8 bytes (64 bits).`;
      * @returns {string}
      */
     runBlowfishEnc: function (input, args) {
-        let key = Cipher._format[args[0].option].parse(args[0].string).toString(Cipher._format.Latin1),
-            mode = args[1],
-            outputFormat = args[2];
+        const key = Utils.convertToByteString(args[0].string, args[0].option),
+            iv = Utils.convertToByteArray(args[1].string, args[1].option),
+            mode = args[2],
+            inputType = args[3],
+            outputType = args[4];
 
         if (key.length === 0) return "Enter a key";
 
-        let encHex = Blowfish.encrypt(input, key, {
-                outputType: 1,
-                cipherMode: Cipher.BLOWFISH_MODES.indexOf(mode)
-            }),
-            enc = CryptoJS.enc.Hex.parse(encHex);
+        input = Utils.convertToByteString(input, inputType);
+
+        Blowfish.setIV(Utils.toBase64(iv), 0);
+
+        const enc = Blowfish.encrypt(input, key, {
+            outputType: Cipher._BLOWFISH_OUTPUT_TYPE_LOOKUP[outputType],
+            cipherMode: Cipher._BLOWFISH_MODE_LOOKUP[mode]
+        });
 
-        return enc.toString(Cipher._format[outputFormat]);
+        return outputType === "Raw" ? Utils.byteArrayToChars(enc) : enc   ;
     },
 
 
@@ -309,18 +336,24 @@ DES uses a key length of 8 bytes (64 bits).`;
      * @returns {string}
      */
     runBlowfishDec: function (input, args) {
-        let key = Cipher._format[args[0].option].parse(args[0].string).toString(Cipher._format.Latin1),
-            mode = args[1],
-            inputFormat = args[2];
+        const key = Utils.convertToByteString(args[0].string, args[0].option),
+            iv = Utils.convertToByteArray(args[1].string, args[1].option),
+            mode = args[2],
+            inputType = args[3],
+            outputType = args[4];
 
         if (key.length === 0) return "Enter a key";
 
-        input = Cipher._format[inputFormat].parse(input);
+        input = inputType === "Raw" ? Utils.strToByteArray(input) : input;
+
+        Blowfish.setIV(Utils.toBase64(iv), 0);
 
-        return Blowfish.decrypt(input.toString(CryptoJS.enc.Base64), key, {
-            outputType: 0, // This actually means inputType. The library is weird.
-            cipherMode: Cipher.BLOWFISH_MODES.indexOf(mode)
+        const result = Blowfish.decrypt(input, key, {
+            outputType: Cipher._BLOWFISH_OUTPUT_TYPE_LOOKUP[inputType], // This actually means inputType. The library is weird.
+            cipherMode: Cipher._BLOWFISH_MODE_LOOKUP[mode]
         });
+
+        return outputType === "Hex" ? Utils.toHexFast(Utils.strToByteArray(result)) : result;
     },