Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance Vigenère Cipher to Support Numeric Characters in Encoding and Decoding #1920

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 15 additions & 18 deletions src/core/operations/VigenèreDecode.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -38,32 +38,29 @@
* @returns {string}
*/
run(input, args) {
const alphabet = "abcdefghijklmnopqrstuvwxyz",
const alphabet = "abcdefghijklmnopqrstuvwxyz0123456789",
key = args[0].toLowerCase();
let output = "",
fail = 0,
keyIndex,
msgIndex,
chr;
let output = "";

Check failure on line 43 in src/core/operations/VigenèreDecode.mjs

View workflow job for this annotation

GitHub Actions / main

Expected indentation of 8 spaces but found 12
let fail = 0;

Check failure on line 44 in src/core/operations/VigenèreDecode.mjs

View workflow job for this annotation

GitHub Actions / main

Expected indentation of 8 spaces but found 12
let keyIndex,msgIndex,chr;

Check failure on line 45 in src/core/operations/VigenèreDecode.mjs

View workflow job for this annotation

GitHub Actions / main

Expected indentation of 8 spaces but found 12

Check failure on line 45 in src/core/operations/VigenèreDecode.mjs

View workflow job for this annotation

GitHub Actions / main

A space is required after ','

Check failure on line 45 in src/core/operations/VigenèreDecode.mjs

View workflow job for this annotation

GitHub Actions / main

A space is required after ','

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();
//Implementing logic

Check failure on line 53 in src/core/operations/VigenèreDecode.mjs

View workflow job for this annotation

GitHub Actions / main

Expected exception block, space or tab after '//' in comment
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++;
}
}
Expand Down
39 changes: 16 additions & 23 deletions src/core/operations/VigenèreEncode.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import OperationError from "../errors/OperationError.mjs";

/**
* Vigenère Encode operation
* Vigenère Encode operation is done
*/
class VigenèreEncode extends Operation {

Expand Down Expand Up @@ -39,36 +39,29 @@
* @returns {string}
*/
run(input, args) {
const alphabet = "abcdefghijklmnopqrstuvwxyz",
const alphabet = "abcdefghijklmnopqrstuvwxyz0123456789",
key = args[0].toLowerCase();
let output = "",
fail = 0,
keyIndex,
msgIndex,
chr;
let output = "";

Check failure on line 44 in src/core/operations/VigenèreEncode.mjs

View workflow job for this annotation

GitHub Actions / main

Expected indentation of 8 spaces but found 12
let fail = 0;

Check failure on line 45 in src/core/operations/VigenèreEncode.mjs

View workflow job for this annotation

GitHub Actions / main

Expected indentation of 8 spaces but found 12
let keyIndex, msgIndex, chr;

Check failure on line 46 in src/core/operations/VigenèreEncode.mjs

View workflow job for this annotation

GitHub Actions / main

Expected indentation of 8 spaces but found 12

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) {
// Get the corresponding character of key for the current letter, accounting
// for chars not in alphabet
const currentChar = input[i];
const lowerChar = currentChar.toLowerCase();
//Implementing logic

Check failure on line 54 in src/core/operations/VigenèreEncode.mjs

View workflow job for this annotation

GitHub Actions / main

Expected exception block, space or tab after '//' in comment
if (alphabet.indexOf(lowerChar) >= 0) {
// Get the corresponding character of key for the current letter
chr = key[(i - fail) % key.length];
// Get the location in the vigenere square of the key char
keyIndex = alphabet.indexOf(chr);
// Get the location in the vigenere square of the message char
msgIndex = alphabet.indexOf(input[i]);
// Get the encoded letter by finding the sum of indexes modulo 26 and finding
// the letter corresponding to that
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();
msgIndex = alphabet.indexOf(lowerChar);
const encodedChar = alphabet[(keyIndex + msgIndex) % alphabet.length];
// Preserve case for letters
output += currentChar === lowerChar ? encodedChar : encodedChar.toUpperCase();
} else {
output += input[i];
output += currentChar; // Keep non-alphabetic characters as is
fail++;
}
}
Expand Down
Loading