1/10/24--VigenereCipher

This commit is contained in:
Saee Biwalkar 2024-10-01 10:30:33 +05:30
parent ea67f796bd
commit fe26185b02
2 changed files with 30 additions and 40 deletions

View file

@ -38,32 +38,29 @@ class VigenèreDecode extends Operation {
* @returns {string} * @returns {string}
*/ */
run(input, args) { run(input, args) {
const alphabet = "abcdefghijklmnopqrstuvwxyz", const alphabet = "abcdefghijklmnopqrstuvwxyz0123456789",
key = args[0].toLowerCase(); key = args[0].toLowerCase();
let output = "", let output = "";
fail = 0, let fail = 0;
keyIndex, let keyIndex,msgIndex,chr;
msgIndex,
chr;
if (!key) throw new OperationError("No key entered"); if (!key) throw new OperationError("No key entered");
if (!/^[a-zA-Z]+$/.test(key)) throw new OperationError("The key must consist only of letters"); if (!/^[a-zA-Z0-9]+$/.test(key)) throw new OperationError("The key must consist only of letters and numbers");
for (let i = 0; i < input.length; i++) { for (let i = 0; i < input.length; i++) {
if (alphabet.indexOf(input[i]) >= 0) { const currentChar = input[i];
const lowerChar = currentChar.toLowerCase();
//Implementing logic
if (alphabet.indexOf(lowerChar) >= 0) {
chr = key[(i - fail) % key.length]; chr = key[(i - fail) % key.length];
keyIndex = alphabet.indexOf(chr); keyIndex = alphabet.indexOf(chr);
msgIndex = alphabet.indexOf(input[i]); msgIndex = alphabet.indexOf(lowerChar);
// Subtract indexes from each other, add 26 just in case the value is negative, // Decode character using the Vigenère formula
// modulo to remove if necessary const decodedChar = alphabet[(msgIndex - keyIndex + alphabet.length) % alphabet.length];
output += alphabet[(msgIndex - keyIndex + alphabet.length) % 26]; // Preserve case for letters and add to output
} else if (alphabet.indexOf(input[i].toLowerCase()) >= 0) { output += currentChar === lowerChar ? decodedChar : decodedChar.toUpperCase();
chr = key[(i - fail) % key.length].toLowerCase();
keyIndex = alphabet.indexOf(chr);
msgIndex = alphabet.indexOf(input[i].toLowerCase());
output += alphabet[(msgIndex + alphabet.length - keyIndex) % 26].toUpperCase();
} else { } else {
output += input[i]; output += currentChar; // Keep non-alphabetic characters as is
fail++; fail++;
} }
} }

View file

@ -39,36 +39,29 @@ class VigenèreEncode extends Operation {
* @returns {string} * @returns {string}
*/ */
run(input, args) { run(input, args) {
const alphabet = "abcdefghijklmnopqrstuvwxyz", const alphabet = "abcdefghijklmnopqrstuvwxyz0123456789",
key = args[0].toLowerCase(); key = args[0].toLowerCase();
let output = "", let output = "";
fail = 0, let fail = 0;
keyIndex, let keyIndex, msgIndex, chr;
msgIndex,
chr;
if (!key) throw new OperationError("No key entered"); if (!key) throw new OperationError("No key entered");
if (!/^[a-zA-Z]+$/.test(key)) throw new OperationError("The key must consist only of letters"); if (!/^[a-zA-Z0-9]+$/.test(key)) throw new OperationError("The key must consist only of letters and numbers");
for (let i = 0; i < input.length; i++) { for (let i = 0; i < input.length; i++) {
if (alphabet.indexOf(input[i]) >= 0) { const currentChar = input[i];
// Get the corresponding character of key for the current letter, accounting const lowerChar = currentChar.toLowerCase();
// for chars not in alphabet //Implementing logic
if (alphabet.indexOf(lowerChar) >= 0) {
// Get the corresponding character of key for the current letter
chr = key[(i - fail) % key.length]; chr = key[(i - fail) % key.length];
// Get the location in the vigenere square of the key char
keyIndex = alphabet.indexOf(chr); keyIndex = alphabet.indexOf(chr);
// Get the location in the vigenere square of the message char msgIndex = alphabet.indexOf(lowerChar);
msgIndex = alphabet.indexOf(input[i]); const encodedChar = alphabet[(keyIndex + msgIndex) % alphabet.length];
// Get the encoded letter by finding the sum of indexes modulo 26 and finding // Preserve case for letters
// the letter corresponding to that output += currentChar === lowerChar ? encodedChar : encodedChar.toUpperCase();
output += alphabet[(keyIndex + msgIndex) % 26];
} else if (alphabet.indexOf(input[i].toLowerCase()) >= 0) {
chr = key[(i - fail) % key.length].toLowerCase();
keyIndex = alphabet.indexOf(chr);
msgIndex = alphabet.indexOf(input[i].toLowerCase());
output += alphabet[(keyIndex + msgIndex) % 26].toUpperCase();
} else { } else {
output += input[i]; output += currentChar; // Keep non-alphabetic characters as is
fail++; fail++;
} }
} }