From 65075020303e91553f2ab73d82cdabcbb94e792f Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni <213192267+Aditya2375@users.noreply.github.com> Date: Tue, 24 Nov 2026 17:51:44 +0530 Subject: [PATCH 1/2] fix: actually reject KeywordShiftedAlphabet keywords with repeated characters checkKeywordValidity returned false inside a forEach callback, which only exits the callback, so it always returned true and repeated-character keywords were accepted. With such a keyword the translation table is not a bijection and encryption loses information: encrypt('HELLO', 'ATTACK AT DAWN') gave 'HSSHLG HS LHVK', which decrypted to 'ATTACK AT CAWN'. Validate the lowercased keyword with every() so each character must be the first occurrence of itself; this also closes the 'Kk' case hole. --- Ciphers/KeywordShiftedAlphabet.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/Ciphers/KeywordShiftedAlphabet.js b/Ciphers/KeywordShiftedAlphabet.js index 2c6f91fe48..a6fbba57e9 100644 --- a/Ciphers/KeywordShiftedAlphabet.js +++ b/Ciphers/KeywordShiftedAlphabet.js @@ -46,13 +46,12 @@ const alphabet = [ ] function checkKeywordValidity(keyword) { - keyword.split('').forEach((char, index) => { - const rest = keyword.slice(0, index) + keyword.slice(index + 1) - if (rest.indexOf(char) !== -1) { - return false - } - }) - return true + // Compare case-insensitively: the translation table is built from the + // lowercased keyword, so 'Kk' would collapse to a duplicate there. + const chars = keyword.toLowerCase().split('') + // forEach cannot return early, so use every: each character must be the + // first occurrence of itself. + return chars.every((char, index) => chars.indexOf(char) === index) } function getEncryptedAlphabet(keyword) { From 8d2cffc971fba2e089a2a3764236c03ca8088450 Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni <213192267+Aditya2375@users.noreply.github.com> Date: Tue, 24 Nov 2026 17:52:52 +0530 Subject: [PATCH 2/2] test: cover KeywordShiftedAlphabet keywords with repeated characters Regression tests for the validation fix: 'HELLO' (repeated L) must throw on encrypt and decrypt, and 'Kk' must throw because the translation table is built from the lowercased keyword. --- Ciphers/test/KeywordShiftedAlphabet.test.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Ciphers/test/KeywordShiftedAlphabet.test.js b/Ciphers/test/KeywordShiftedAlphabet.test.js index 38e0839488..27307b3b34 100644 --- a/Ciphers/test/KeywordShiftedAlphabet.test.js +++ b/Ciphers/test/KeywordShiftedAlphabet.test.js @@ -11,3 +11,15 @@ test('The Algorithms === decrypt(encrypt(The Algorithms))', () => { const result = decrypt('keyword', encrypt('keyword', word)) expect(result).toMatch(word) }) + +test('rejects keywords with repeated characters', () => { + // 'HELLO' repeats L; encrypt must throw instead of producing + // ciphertext that cannot be decrypted back + expect(() => encrypt('HELLO', 'ATTACK AT DAWN')).toThrow('Invalid keyword!') + expect(() => decrypt('HELLO', 'HSSHLG HS LHVK')).toThrow('Invalid keyword!') +}) + +test('rejects keywords that repeat a character in another case', () => { + // the table is built from the lowercased keyword, so 'Kk' is a duplicate + expect(() => encrypt('Kk', 'ATTACK')).toThrow('Invalid keyword!') +})