This commit is contained in:
Andy Wang 2019-09-26 19:47:42 +01:00
parent b1edcffcc0
commit 3c5192c2bf
2 changed files with 37 additions and 37 deletions

View file

@ -64,8 +64,8 @@ class DESDecrypt extends Operation {
*/ */
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),
[,, mode, inputType, outputType] = args; [,, mode, inputType, outputType] = args;
var iv = Utils.convertToByteArray(args[1].string, args[1].option)
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
@ -77,36 +77,36 @@ Triple DES uses a key length of 24 bytes (192 bits).`);
input = Utils.convertToByteString(input, inputType); input = Utils.convertToByteString(input, inputType);
const decipher = forge.cipher.createDecipher("DES-" + mode, key); const decipher = forge.cipher.createDecipher("DES-" + mode, key);
var result let result;
if (mode === "CTR"){ if (mode === "CTR") {
// Temp workaround until https://github.com/digitalbazaar/forge/issues/721 is fixed // Temp workaround until https://github.com/digitalbazaar/forge/issues/721 is fixed
const blockSize = decipher.mode.blockSize const blockSize = decipher.mode.blockSize;
var blockOutputs = forge.util.createBuffer(); const blockOutputs = forge.util.createBuffer();
var numBlocks = input.length % blockSize === 0 ? input.length >> 3 : (input.length >> 3) + 1 const numBlocks = input.length % blockSize === 0 ? input.length >> 3 : (input.length >> 3) + 1;
if (iv.length < blockSize) { if (iv.length < blockSize) {
var ivLen = iv.length const ivLen = iv.length;
for (var i=0; i < blockSize - ivLen; i++){ for (let i=0; i < blockSize - ivLen; i++) {
iv.unshift(0) iv.unshift(0);
} }
} }
for (var i=0; i < numBlocks; i++) { for (let j=0; j < numBlocks; j++) {
decipher.start({iv: iv}) decipher.start({iv: iv});
decipher.update(forge.util.createBuffer().fillWithByte(0,blockSize)) decipher.update(forge.util.createBuffer().fillWithByte(0, blockSize));
blockOutputs.putBuffer(decipher.output) blockOutputs.putBuffer(decipher.output);
iv[iv.length-1] = (iv[iv.length-1] + 1) & 0xFFFFFFFF iv[iv.length-1] = (iv[iv.length-1] + 1) & 0xFFFFFFFF;
} }
var output = forge.util.createBuffer() const output = forge.util.createBuffer();
for (var i=0; i < input.length; i++) { for (let k=0; k < input.length; k++) {
output.putByte(input.charCodeAt(i)^blockOutputs.getByte()) output.putByte(input.charCodeAt(k)^blockOutputs.getByte());
} }
result = true result = true;
decipher.output = output decipher.output = output;
} else { } else {
decipher.start({iv: iv}); decipher.start({iv: iv});
decipher.update(forge.util.createBuffer(input)); decipher.update(forge.util.createBuffer(input));
result = decipher.finish(); result = decipher.finish();
} }
if (result) { if (result) {

View file

@ -64,8 +64,8 @@ class DESEncrypt extends Operation {
*/ */
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),
[,, mode, inputType, outputType] = args; [,, mode, inputType, outputType] = args;
var iv = Utils.convertToByteArray(args[1].string, args[1].option)
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
@ -78,29 +78,29 @@ Triple DES uses a key length of 24 bytes (192 bits).`);
const cipher = forge.cipher.createCipher("DES-" + mode, key); const cipher = forge.cipher.createCipher("DES-" + mode, key);
if (mode === "CTR"){ if (mode === "CTR") {
// Temp workaround until https://github.com/digitalbazaar/forge/issues/721 is fixed // Temp workaround until https://github.com/digitalbazaar/forge/issues/721 is fixed
const blockSize = cipher.mode.blockSize const blockSize = cipher.mode.blockSize;
var blockOutputs = forge.util.createBuffer(); const blockOutputs = forge.util.createBuffer();
var numBlocks = input.length % blockSize === 0 ? input.length >> 3 : (input.length >> 3) + 1 const numBlocks = input.length % blockSize === 0 ? input.length >> 3 : (input.length >> 3) + 1;
if (iv.length < blockSize) { if (iv.length < blockSize) {
var ivLen = iv.length const ivLen = iv.length;
for (var i=0; i < blockSize - ivLen; i++){ for (let i=0; i < blockSize - ivLen; i++) {
iv.unshift(0) iv.unshift(0);
} }
} }
for (var i=0; i < numBlocks; i++) { for (let j=0; j < numBlocks; j++) {
cipher.start({iv: iv}) cipher.start({iv: iv});
cipher.update(forge.util.createBuffer().fillWithByte(0,blockSize)) cipher.update(forge.util.createBuffer().fillWithByte(0, blockSize));
blockOutputs.putBuffer(cipher.output) blockOutputs.putBuffer(cipher.output);
iv[iv.length-1] = (iv[iv.length-1] + 1) & 0xFFFFFFFF iv[iv.length-1] = (iv[iv.length-1] + 1) & 0xFFFFFFFF;
} }
var output = forge.util.createBuffer() const output = forge.util.createBuffer();
for (var i=0; i < input.length; i++) { for (let k=0; k < input.length; k++) {
output.putByte(input.charCodeAt(i)^blockOutputs.getByte()) output.putByte(input.charCodeAt(k)^blockOutputs.getByte());
} }
cipher.output=output cipher.output=output;
} else { } else {
cipher.start({iv: iv}); cipher.start({iv: iv});
cipher.update(forge.util.createBuffer(input)); cipher.update(forge.util.createBuffer(input));