mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-20 14:56:19 -04:00
Cleaned up the Vigenere cipher operations so that they conform with the coding conventions.
This commit is contained in:
parent
e5854b6f86
commit
55c1626d41
7 changed files with 95 additions and 100 deletions
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -80,10 +80,10 @@ var Categories = [
|
||||||
"ROT47",
|
"ROT47",
|
||||||
"XOR",
|
"XOR",
|
||||||
"XOR Brute Force",
|
"XOR Brute Force",
|
||||||
|
"Vigenère Encode",
|
||||||
|
"Vigenère Decode",
|
||||||
"Derive PBKDF2 key",
|
"Derive PBKDF2 key",
|
||||||
"Derive EVP key",
|
"Derive EVP key",
|
||||||
"Vigenere Encode",
|
|
||||||
"Vigenere Decode"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
|
@ -1325,33 +1325,33 @@ var OperationConfig = {
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"Vigenere Encode": {
|
"Vigenère Encode": {
|
||||||
description: "Encodes string with the Vigenere cipher.",
|
description: "The Vigenere cipher is a method of encrypting alphabetic text by using a series of different Caesar ciphers based on the letters of a keyword. It is a simple form of polyalphabetic substitution.",
|
||||||
run: Cipher.run_vigenc,
|
run: Cipher.run_vigenere_enc,
|
||||||
highlight: true,
|
highlight: true,
|
||||||
highlight_reverse: true,
|
highlight_reverse: true,
|
||||||
input_type: "string",
|
input_type: "string",
|
||||||
output_type: "string",
|
output_type: "string",
|
||||||
args: [
|
args: [
|
||||||
{
|
{
|
||||||
name: "Keyword",
|
name: "Key",
|
||||||
type: "string",
|
type: "string",
|
||||||
value: Cipher.VIG_ENC_KEY
|
value: ""
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"Vigenere Decode": {
|
"Vigenère Decode": {
|
||||||
description: "Decodes string with the Vigenere cipher.",
|
description: "The Vigenere cipher is a method of encrypting alphabetic text by using a series of different Caesar ciphers based on the letters of a keyword. It is a simple form of polyalphabetic substitution.",
|
||||||
run: Cipher.run_vigdec,
|
run: Cipher.run_vigenere_dec,
|
||||||
highlight: true,
|
highlight: true,
|
||||||
highlight_reverse: true,
|
highlight_reverse: true,
|
||||||
input_type: "string",
|
input_type: "string",
|
||||||
output_type: "string",
|
output_type: "string",
|
||||||
args: [
|
args: [
|
||||||
{
|
{
|
||||||
name: "Keyword",
|
name: "Key",
|
||||||
type: "string",
|
type: "string",
|
||||||
value: Cipher.VIG_DEC_KEY
|
value: ""
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|
|
@ -387,101 +387,94 @@ var Cipher = {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @constant
|
* Vigenère Encode operation.
|
||||||
* @default
|
|
||||||
*/
|
|
||||||
VIG_ENC_KEY: "cipher",
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Vigenere cipher encode.
|
|
||||||
*
|
*
|
||||||
* @author Matt C [matt@artemisbot.pw]
|
* @author Matt C [matt@artemisbot.pw]
|
||||||
* @param {string} input
|
* @param {string} input
|
||||||
* @param {Object[]} args
|
* @param {Object[]} args
|
||||||
* @returns {string}
|
* @returns {string}
|
||||||
*/
|
*/
|
||||||
run_vigenc: function (input, args) {
|
run_vigenere_enc: function (input, args) {
|
||||||
var alphabet = "abcdefghijklmnopqrstuvwxyz",
|
var alphabet = "abcdefghijklmnopqrstuvwxyz",
|
||||||
keyword = args[0].toLowerCase(),
|
key = args[0].toLowerCase(),
|
||||||
output = "",
|
output = "",
|
||||||
fail = 0,
|
fail = 0,
|
||||||
keyIndex,
|
key_index,
|
||||||
msgIndex,
|
msg_index,
|
||||||
chr;
|
chr;
|
||||||
if (keyword) {
|
|
||||||
if (/^[a-zA-Z]+$/.test(keyword)) {
|
if (!key) return "No key entered";
|
||||||
for (var i = 0; i < input.length; i++) {
|
if (!/^[a-zA-Z]+$/.test(key)) return "The key must consist only of letters";
|
||||||
if (alphabet.indexOf(input[i]) >= 0) {
|
|
||||||
chr = keyword[(i - fail) % keyword.length]; //Gets the corresponding character of keyword for current letter, accounting for chars not in alphabet
|
for (var i = 0; i < input.length; i++) {
|
||||||
keyIndex = alphabet.indexOf(chr); //Gets location in vigenere square of keyword char
|
if (alphabet.indexOf(input[i]) >= 0) {
|
||||||
msgIndex = alphabet.indexOf(input[i]); //Gets location in vigenere square of message char
|
// Get the corresponding character of key for the current letter, accounting
|
||||||
output += alphabet[(keyIndex + msgIndex) % 26]; //Gets encoded letter by finding sum of indexes modulo 26 and finding the letter corresponding to that
|
// for chars not in alphabet
|
||||||
} else if (alphabet.indexOf(input[i].toLowerCase()) >= 0) {
|
chr = key[(i - fail) % key.length];
|
||||||
chr = keyword[(i - fail) % keyword.length].toLowerCase();
|
// Get the location in the vigenere square of the key char
|
||||||
keyIndex = alphabet.indexOf(chr);
|
key_index = alphabet.indexOf(chr);
|
||||||
msgIndex = alphabet.indexOf(input[i].toLowerCase());
|
// Get the location in the vigenere square of the message char
|
||||||
output += alphabet[(keyIndex + msgIndex) % 26].toUpperCase();
|
msg_index = alphabet.indexOf(input[i]);
|
||||||
} else {
|
// Get the encoded letter by finding the sum of indexes modulo 26 and finding
|
||||||
output += input[i];
|
// the letter corresponding to that
|
||||||
fail++;
|
output += alphabet[(key_index + msg_index) % 26];
|
||||||
}
|
} else if (alphabet.indexOf(input[i].toLowerCase()) >= 0) {
|
||||||
}
|
chr = key[(i - fail) % key.length].toLowerCase();
|
||||||
|
key_index = alphabet.indexOf(chr);
|
||||||
|
msg_index = alphabet.indexOf(input[i].toLowerCase());
|
||||||
|
output += alphabet[(key_index + msg_index) % 26].toUpperCase();
|
||||||
} else {
|
} else {
|
||||||
throw "Keyword can only consist of letters.";
|
output += input[i];
|
||||||
|
fail++;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
throw "A keyword is required.";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
},
|
},
|
||||||
/**
|
|
||||||
* @constant
|
|
||||||
* @default
|
|
||||||
*/
|
|
||||||
VIG_DEC_KEY: "cipher",
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Vigenere cipher decode.
|
* Vigenère Decode operation.
|
||||||
*
|
*
|
||||||
* @author Matt C [matt@artemisbot.pw]
|
* @author Matt C [matt@artemisbot.pw]
|
||||||
* @param {string} input
|
* @param {string} input
|
||||||
* @param {Object[]} args
|
* @param {Object[]} args
|
||||||
* @returns {string}
|
* @returns {string}
|
||||||
*/
|
*/
|
||||||
run_vigdec: function (input, args) {
|
run_vigenere_dec: function (input, args) {
|
||||||
var alphabet = "abcdefghijklmnopqrstuvwxyz",
|
var alphabet = "abcdefghijklmnopqrstuvwxyz",
|
||||||
keyword = args[0].toLowerCase(),
|
key = args[0].toLowerCase(),
|
||||||
output = "",
|
output = "",
|
||||||
fail = 0,
|
fail = 0,
|
||||||
keyIndex,
|
key_index,
|
||||||
msgIndex,
|
msg_index,
|
||||||
chr;
|
chr;
|
||||||
if (keyword) {
|
|
||||||
if (/^[a-zA-Z]+$/.test(keyword)) {
|
if (!key) return "No key entered";
|
||||||
for (var i = 0; i < input.length; i++) {
|
if (!/^[a-zA-Z]+$/.test(key)) return "The key must consist only of letters";
|
||||||
if (alphabet.indexOf(input[i]) >= 0) {
|
|
||||||
chr = keyword[(i - fail) % keyword.length];
|
for (var i = 0; i < input.length; i++) {
|
||||||
keyIndex = alphabet.indexOf(chr);
|
if (alphabet.indexOf(input[i]) >= 0) {
|
||||||
msgIndex = alphabet.indexOf(input[i]);
|
chr = key[(i - fail) % key.length];
|
||||||
output += alphabet[(msgIndex - keyIndex + alphabet.length ) % 26]; //subtract indexes from each other, add 26 just in case the value is negative, modulo to remove if neccessary
|
key_index = alphabet.indexOf(chr);
|
||||||
} else if (alphabet.indexOf(input[i].toLowerCase()) >= 0) {
|
msg_index = alphabet.indexOf(input[i]);
|
||||||
chr = keyword[(i - fail) % keyword.length].toLowerCase();
|
// Subtract indexes from each other, add 26 just in case the value is negative,
|
||||||
keyIndex = alphabet.indexOf(chr);
|
// modulo to remove if neccessary
|
||||||
msgIndex = alphabet.indexOf(input[i].toLowerCase());
|
output += alphabet[(msg_index - key_index + alphabet.length ) % 26];
|
||||||
output += alphabet[(msgIndex + alphabet.length - keyIndex) % 26].toUpperCase();
|
} else if (alphabet.indexOf(input[i].toLowerCase()) >= 0) {
|
||||||
} else {
|
chr = key[(i - fail) % key.length].toLowerCase();
|
||||||
output += input[i];
|
key_index = alphabet.indexOf(chr);
|
||||||
fail++;
|
msg_index = alphabet.indexOf(input[i].toLowerCase());
|
||||||
}
|
output += alphabet[(msg_index + alphabet.length - key_index) % 26].toUpperCase();
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
throw "Keyword can only consist of letters.";
|
output += input[i];
|
||||||
|
fail++;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
throw "A keyword is required.";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
}
|
},
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
202 source files
|
203 source files
|
||||||
104281 lines
|
104403 lines
|
||||||
4.0M size
|
4.0M size
|
||||||
|
|
||||||
136 JavaScript source files
|
136 JavaScript source files
|
||||||
95191 lines
|
95313 lines
|
||||||
3.5M size
|
3.5M size
|
||||||
|
|
||||||
78 third party JavaScript source files
|
78 third party JavaScript source files
|
||||||
|
@ -11,11 +11,11 @@
|
||||||
2.7M size
|
2.7M size
|
||||||
|
|
||||||
58 first party JavaScript source files
|
58 first party JavaScript source files
|
||||||
18814 lines
|
18936 lines
|
||||||
728K size
|
728K size
|
||||||
|
|
||||||
3.2M uncompressed JavaScript size
|
3.2M uncompressed JavaScript size
|
||||||
1.7M compressed JavaScript size
|
1.7M compressed JavaScript size
|
||||||
|
|
||||||
15 categories
|
15 categories
|
||||||
153 operations
|
155 operations
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue