mirror of
https://github.com/gchq/CyberChef
synced 2025-01-04 00:38:41 +00:00
Merge branch 'SamueleFacendaSubstitution' of https://github.com/SamueleFacenda/CyberChef
This commit is contained in:
commit
743b834f6d
1 changed files with 50 additions and 8 deletions
|
@ -34,10 +34,49 @@ class Substitute extends Operation {
|
||||||
"name": "Ciphertext",
|
"name": "Ciphertext",
|
||||||
"type": "binaryString",
|
"type": "binaryString",
|
||||||
"value": "XYZABCDEFGHIJKLMNOPQRSTUVW"
|
"value": "XYZABCDEFGHIJKLMNOPQRSTUVW"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Ignore case",
|
||||||
|
"type": "boolean",
|
||||||
|
"value": false
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert a single character using the dictionary, if ignoreCase is true then
|
||||||
|
* check in the dictionary for both upper and lower case versions of the character.
|
||||||
|
* In output the input character case is preserved.
|
||||||
|
* @param {string} char
|
||||||
|
* @param {Object} dict
|
||||||
|
* @param {boolean} ignoreCase
|
||||||
|
* @returns {string}
|
||||||
|
*/
|
||||||
|
cipherSingleChar(char, dict, ignoreCase) {
|
||||||
|
if (!ignoreCase)
|
||||||
|
return dict[char] || char;
|
||||||
|
|
||||||
|
const isUpperCase = char === char.toUpperCase();
|
||||||
|
|
||||||
|
// convert using the dictionary keeping the case of the input character
|
||||||
|
|
||||||
|
if (dict[char] !== undefined)
|
||||||
|
// if the character is in the dictionary return the value with the input case
|
||||||
|
return isUpperCase ? dict[char].toUpperCase() : dict[char].toLowerCase();
|
||||||
|
|
||||||
|
// check for the other case, if it is in the dictionary return the value with the right case
|
||||||
|
if (isUpperCase) {
|
||||||
|
if (dict[char.toLowerCase()] !== undefined)
|
||||||
|
return dict[char.toLowerCase()].toUpperCase();
|
||||||
|
} else {
|
||||||
|
if (dict[char.toUpperCase()] !== undefined)
|
||||||
|
return dict[char.toUpperCase()].toLowerCase();
|
||||||
|
}
|
||||||
|
|
||||||
|
return char;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} input
|
* @param {string} input
|
||||||
* @param {Object[]} args
|
* @param {Object[]} args
|
||||||
|
@ -46,17 +85,20 @@ class Substitute extends Operation {
|
||||||
run(input, args) {
|
run(input, args) {
|
||||||
const plaintext = Utils.expandAlphRange([...args[0]]),
|
const plaintext = Utils.expandAlphRange([...args[0]]),
|
||||||
ciphertext = Utils.expandAlphRange([...args[1]]);
|
ciphertext = Utils.expandAlphRange([...args[1]]);
|
||||||
let output = "",
|
let output = "";
|
||||||
index = -1;
|
const ignoreCase = args[2];
|
||||||
|
|
||||||
if (plaintext.length !== ciphertext.length) {
|
if (plaintext.length !== ciphertext.length)
|
||||||
output = "Warning: Plaintext and Ciphertext lengths differ\n\n";
|
output = "Warning: Plaintext and Ciphertext lengths differ\n\n";
|
||||||
}
|
|
||||||
|
|
||||||
for (const character of input) {
|
// create dictionary for conversion
|
||||||
index = plaintext.indexOf(character);
|
const dict = {};
|
||||||
output += index > -1 && index < ciphertext.length ? ciphertext[index] : character;
|
for (let i = 0; i < Math.min(ciphertext.length, plaintext.length); i++)
|
||||||
}
|
dict[plaintext[i]] = ciphertext[i];
|
||||||
|
|
||||||
|
// map every letter with the conversion function
|
||||||
|
for (const character of input)
|
||||||
|
output += this.cipherSingleChar(character, dict, ignoreCase);
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue