RC5 and RC6

This commit is contained in:
Saee Biwalkar 2024-09-24 10:13:47 +05:30
parent 887a0421f4
commit 0a40a97a67
5 changed files with 717 additions and 20 deletions

View file

@ -6,6 +6,7 @@
import Operation from "../Operation.mjs";
import OperationError from "../errors/OperationError.mjs";
/**
* Vigenère Decode operation
*/
@ -38,32 +39,31 @@ class VigenèreDecode extends Operation {
* @returns {string}
*/
run(input, args) {
const alphabet = "abcdefghijklmnopqrstuvwxyz",
key = args[0].toLowerCase();
let output = "",
fail = 0,
keyIndex,
msgIndex,
chr;
const alphabet = "abcdefghijklmnopqrstuvwxyz0123456789";
const key = args[0].toLowerCase();
let output = "";
let fail = 0;
let keyIndex, msgIndex, chr;
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++) {
if (alphabet.indexOf(input[i]) >= 0) {
const currentChar = input[i];
const lowerChar = currentChar.toLowerCase();
if (alphabet.indexOf(lowerChar) >= 0) {
chr = key[(i - fail) % key.length];
keyIndex = alphabet.indexOf(chr);
msgIndex = alphabet.indexOf(input[i]);
// Subtract indexes from each other, add 26 just in case the value is negative,
// modulo to remove if necessary
output += alphabet[(msgIndex - keyIndex + alphabet.length) % 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[(msgIndex + alphabet.length - keyIndex) % 26].toUpperCase();
msgIndex = alphabet.indexOf(lowerChar);
// Decode character using the Vigenère formula
const decodedChar = alphabet[(msgIndex - keyIndex + alphabet.length) % alphabet.length];
// Preserve case for letters and add to output
output += currentChar === lowerChar ? decodedChar : decodedChar.toUpperCase();
} else {
output += input[i];
output += currentChar; // Keep non-alphabetic characters as is
fail++;
}
}
@ -96,7 +96,6 @@ class VigenèreDecode extends Operation {
highlightReverse(pos, args) {
return pos;
}
}
export default VigenèreDecode;