diff --git a/src/core/operations/DESDecrypt.mjs b/src/core/operations/DESDecrypt.mjs index 14bbe832..95c01a3d 100644 --- a/src/core/operations/DESDecrypt.mjs +++ b/src/core/operations/DESDecrypt.mjs @@ -53,6 +53,11 @@ class DESDecrypt extends Operation { "name": "Output", "type": "option", "value": ["Raw", "Hex"] + }, + { + "name": "Key Padding", + "type": "option", + "value": ["None", "Null", "Repeat"] } ]; } @@ -63,9 +68,13 @@ class DESDecrypt extends Operation { * @returns {string} */ run(input, args) { - const key = Utils.convertToByteString(args[0].string, args[0].option), - iv = Utils.convertToByteArray(args[1].string, args[1].option), - [,, mode, inputType, outputType] = args; + const key = Utils.convertToByteString(args[0].string, args[0].option); + var iv = Utils.convertToByteArray(args[1].string, args[1].option), + [,, mode, inputType, outputType, keyPadding] = args; + + if (keyPadding !== "None" && key.length < 8) { + key = key.padEnd(8, keyPadding == "Null" ? "\0" : key); + } if (key.length !== 8) { throw new OperationError(`Invalid key length: ${key.length} bytes diff --git a/src/core/operations/DESEncrypt.mjs b/src/core/operations/DESEncrypt.mjs index 9472abe8..eb834e2b 100644 --- a/src/core/operations/DESEncrypt.mjs +++ b/src/core/operations/DESEncrypt.mjs @@ -53,6 +53,11 @@ class DESEncrypt extends Operation { "name": "Output", "type": "option", "value": ["Hex", "Raw"] + }, + { + "name": "Key Padding", + "type": "option", + "value": ["None", "Null", "Repeat"] } ]; } @@ -63,9 +68,13 @@ class DESEncrypt extends Operation { * @returns {string} */ run(input, args) { - const key = Utils.convertToByteString(args[0].string, args[0].option), - iv = Utils.convertToByteArray(args[1].string, args[1].option), - [,, mode, inputType, outputType] = args; + var key = Utils.convertToByteString(args[0].string, args[0].option); + const iv = Utils.convertToByteArray(args[1].string, args[1].option), + [,, mode, inputType, outputType, keyPadding] = args; + + if (keyPadding !== "None" && key.length < 8) { + key = key.padEnd(8, keyPadding == "Null" ? "\0" : key); + } if (key.length !== 8) { throw new OperationError(`Invalid key length: ${key.length} bytes diff --git a/tests/operations/tests/Crypt.mjs b/tests/operations/tests/Crypt.mjs index 3cf7b27e..6675fda6 100644 --- a/tests/operations/tests/Crypt.mjs +++ b/tests/operations/tests/Crypt.mjs @@ -619,7 +619,7 @@ Triple DES uses a key length of 24 bytes (192 bits).`, "args": [ {"option": "Hex", "string": ""}, {"option": "Hex", "string": ""}, - "CBC", "Hex", "Hex" + "CBC", "Hex", "Hex", "None" ] } ], @@ -634,7 +634,7 @@ Triple DES uses a key length of 24 bytes (192 bits).`, "args": [ {"option": "Hex", "string": "58345efb0a64e87e"}, {"option": "Hex", "string": "533ed1378bfd929e"}, - "CBC", "Hex", "Hex" + "CBC", "Hex", "Hex", "None" ] } ], @@ -649,7 +649,7 @@ Triple DES uses a key length of 24 bytes (192 bits).`, "args": [ {"option": "Hex", "string": "58345efb0a64e87e"}, {"option": "Hex", "string": "533ed1378bfd929e"}, - "CFB", "Hex", "Hex" + "CFB", "Hex", "Hex", "None" ] } ], @@ -664,7 +664,7 @@ Triple DES uses a key length of 24 bytes (192 bits).`, "args": [ {"option": "Hex", "string": "58345efb0a64e87e"}, {"option": "Hex", "string": "533ed1378bfd929e"}, - "OFB", "Hex", "Hex" + "OFB", "Hex", "Hex", "None" ] } ], @@ -680,7 +680,7 @@ Triple DES uses a key length of 24 bytes (192 bits).`, "args": [ {"option": "Hex", "string": "58345efb0a64e87e"}, {"option": "Hex", "string": "533ed1378bfd929e"}, - "CTR", "Hex", "Hex" + "CTR", "Hex", "Hex", "None" ] } ], @@ -695,7 +695,7 @@ Triple DES uses a key length of 24 bytes (192 bits).`, "args": [ {"option": "Hex", "string": "58345efb0a64e87e"}, {"option": "Hex", "string": "533ed1378bfd929e"}, - "ECB", "Hex", "Hex" + "ECB", "Hex", "Hex", "None" ] } ], @@ -1370,7 +1370,7 @@ Triple DES uses a key length of 24 bytes (192 bits).`, "args": [ {"option": "Hex", "string": ""}, {"option": "Hex", "string": ""}, - "CBC", "Hex", "Hex" + "CBC", "Hex", "Hex", "None" ] } ], @@ -1385,7 +1385,7 @@ Triple DES uses a key length of 24 bytes (192 bits).`, "args": [ {"option": "Hex", "string": "58345efb0a64e87e"}, {"option": "Hex", "string": "533ed1378bfd929e"}, - "CBC", "Hex", "Hex" + "CBC", "Hex", "Hex", "None" ] } ], @@ -1400,7 +1400,7 @@ Triple DES uses a key length of 24 bytes (192 bits).`, "args": [ {"option": "Hex", "string": "58345efb0a64e87e"}, {"option": "Hex", "string": "533ed1378bfd929e"}, - "CFB", "Hex", "Hex" + "CFB", "Hex", "Hex", "None" ] } ], @@ -1415,7 +1415,7 @@ Triple DES uses a key length of 24 bytes (192 bits).`, "args": [ {"option": "Hex", "string": "58345efb0a64e87e"}, {"option": "Hex", "string": "533ed1378bfd929e"}, - "OFB", "Hex", "Hex" + "OFB", "Hex", "Hex", "None" ] } ], @@ -1431,7 +1431,7 @@ Triple DES uses a key length of 24 bytes (192 bits).`, "args": [ {"option": "Hex", "string": "58345efb0a64e87e"}, {"option": "Hex", "string": "533ed1378bfd929e"}, - "CTR", "Hex", "Hex" + "CTR", "Hex", "Hex", "None" ] } ], @@ -1446,7 +1446,7 @@ Triple DES uses a key length of 24 bytes (192 bits).`, "args": [ {"option": "Hex", "string": "58345efb0a64e87e"}, {"option": "Hex", "string": "533ed1378bfd929e"}, - "ECB", "Hex", "Hex" + "ECB", "Hex", "Hex", "None" ] } ],