Added 'Key Padding' to DES

This commit is contained in:
Michael Rowley 2022-03-22 23:15:03 +00:00
parent a811041fe2
commit 99afd014f2
3 changed files with 36 additions and 18 deletions

View file

@ -53,6 +53,11 @@ class DESDecrypt extends Operation {
"name": "Output", "name": "Output",
"type": "option", "type": "option",
"value": ["Raw", "Hex"] "value": ["Raw", "Hex"]
},
{
"name": "Key Padding",
"type": "option",
"value": ["None", "Null", "Repeat"]
} }
]; ];
} }
@ -63,9 +68,13 @@ class DESDecrypt extends Operation {
* @returns {string} * @returns {string}
*/ */
run(input, args) { run(input, args) {
const key = Utils.convertToByteString(args[0].string, args[0].option), const key = Utils.convertToByteString(args[0].string, args[0].option);
iv = Utils.convertToByteArray(args[1].string, args[1].option), var iv = Utils.convertToByteArray(args[1].string, args[1].option),
[,, mode, inputType, outputType] = args; [,, mode, inputType, outputType, keyPadding] = args;
if (keyPadding !== "None" && key.length < 8) {
key = key.padEnd(8, keyPadding == "Null" ? "\0" : key);
}
if (key.length !== 8) { if (key.length !== 8) {
throw new OperationError(`Invalid key length: ${key.length} bytes throw new OperationError(`Invalid key length: ${key.length} bytes

View file

@ -53,6 +53,11 @@ class DESEncrypt extends Operation {
"name": "Output", "name": "Output",
"type": "option", "type": "option",
"value": ["Hex", "Raw"] "value": ["Hex", "Raw"]
},
{
"name": "Key Padding",
"type": "option",
"value": ["None", "Null", "Repeat"]
} }
]; ];
} }
@ -63,9 +68,13 @@ class DESEncrypt extends Operation {
* @returns {string} * @returns {string}
*/ */
run(input, args) { run(input, args) {
const key = Utils.convertToByteString(args[0].string, args[0].option), var key = Utils.convertToByteString(args[0].string, args[0].option);
iv = Utils.convertToByteArray(args[1].string, args[1].option), const iv = Utils.convertToByteArray(args[1].string, args[1].option),
[,, mode, inputType, outputType] = args; [,, mode, inputType, outputType, keyPadding] = args;
if (keyPadding !== "None" && key.length < 8) {
key = key.padEnd(8, keyPadding == "Null" ? "\0" : key);
}
if (key.length !== 8) { if (key.length !== 8) {
throw new OperationError(`Invalid key length: ${key.length} bytes throw new OperationError(`Invalid key length: ${key.length} bytes

View file

@ -619,7 +619,7 @@ Triple DES uses a key length of 24 bytes (192 bits).`,
"args": [ "args": [
{"option": "Hex", "string": ""}, {"option": "Hex", "string": ""},
{"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": [ "args": [
{"option": "Hex", "string": "58345efb0a64e87e"}, {"option": "Hex", "string": "58345efb0a64e87e"},
{"option": "Hex", "string": "533ed1378bfd929e"}, {"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": [ "args": [
{"option": "Hex", "string": "58345efb0a64e87e"}, {"option": "Hex", "string": "58345efb0a64e87e"},
{"option": "Hex", "string": "533ed1378bfd929e"}, {"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": [ "args": [
{"option": "Hex", "string": "58345efb0a64e87e"}, {"option": "Hex", "string": "58345efb0a64e87e"},
{"option": "Hex", "string": "533ed1378bfd929e"}, {"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": [ "args": [
{"option": "Hex", "string": "58345efb0a64e87e"}, {"option": "Hex", "string": "58345efb0a64e87e"},
{"option": "Hex", "string": "533ed1378bfd929e"}, {"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": [ "args": [
{"option": "Hex", "string": "58345efb0a64e87e"}, {"option": "Hex", "string": "58345efb0a64e87e"},
{"option": "Hex", "string": "533ed1378bfd929e"}, {"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": [ "args": [
{"option": "Hex", "string": ""}, {"option": "Hex", "string": ""},
{"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": [ "args": [
{"option": "Hex", "string": "58345efb0a64e87e"}, {"option": "Hex", "string": "58345efb0a64e87e"},
{"option": "Hex", "string": "533ed1378bfd929e"}, {"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": [ "args": [
{"option": "Hex", "string": "58345efb0a64e87e"}, {"option": "Hex", "string": "58345efb0a64e87e"},
{"option": "Hex", "string": "533ed1378bfd929e"}, {"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": [ "args": [
{"option": "Hex", "string": "58345efb0a64e87e"}, {"option": "Hex", "string": "58345efb0a64e87e"},
{"option": "Hex", "string": "533ed1378bfd929e"}, {"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": [ "args": [
{"option": "Hex", "string": "58345efb0a64e87e"}, {"option": "Hex", "string": "58345efb0a64e87e"},
{"option": "Hex", "string": "533ed1378bfd929e"}, {"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": [ "args": [
{"option": "Hex", "string": "58345efb0a64e87e"}, {"option": "Hex", "string": "58345efb0a64e87e"},
{"option": "Hex", "string": "533ed1378bfd929e"}, {"option": "Hex", "string": "533ed1378bfd929e"},
"ECB", "Hex", "Hex" "ECB", "Hex", "Hex", "None"
] ]
} }
], ],