From 88465e4effbd71ba690ec4f9fbbf69e858d9812d Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni <213192267+Aditya2375@users.noreply.github.com> Date: Tue, 24 Nov 2026 15:57:55 +0530 Subject: [PATCH 1/2] fix: normalize Playfair key before building the matrix encrypt() normalizes the plaintext (uppercase, J folded into I, non-letters stripped) but the constructor passed the raw key straight into the matrix builder. A lowercase key like "monarchy" or one containing J or punctuation produced a wrong matrix, so ciphertext was garbage and Z could never round-trip. Normalize the key the same way before building the matrix. --- src/main/java/com/thealgorithms/ciphers/PlayfairCipher.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/ciphers/PlayfairCipher.java b/src/main/java/com/thealgorithms/ciphers/PlayfairCipher.java index 76ceb6dbce31..3e29ea489dd8 100644 --- a/src/main/java/com/thealgorithms/ciphers/PlayfairCipher.java +++ b/src/main/java/com/thealgorithms/ciphers/PlayfairCipher.java @@ -6,7 +6,11 @@ public class PlayfairCipher { private String key; public PlayfairCipher(String key) { - this.key = key; + // Normalize the key the same way the plaintext is normalized in encrypt(): + // uppercase, J folded into I, and non-letters removed. Without this a + // lowercase or mixed-case key, a key containing J, or a key with + // punctuation silently produces a wrong matrix. + this.key = key.toUpperCase().replace("J", "I").replaceAll("[^A-Z]", ""); generateMatrix(); } From 03c0c27ed438b6f80cce30ffc631487c1b0963c0 Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni <213192267+Aditya2375@users.noreply.github.com> Date: Tue, 24 Nov 2026 15:59:18 +0530 Subject: [PATCH 2/2] test: cover Playfair keys that are lowercase, contain J, or have non-letters Regression tests for the key-normalization fix: a lowercase key must behave like its uppercase form, a key containing J must still cover Z in the matrix, and punctuation or digits in the key must be ignored. --- .../thealgorithms/ciphers/PlayfairTest.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/test/java/com/thealgorithms/ciphers/PlayfairTest.java b/src/test/java/com/thealgorithms/ciphers/PlayfairTest.java index fa497e4682e8..2386aa735b7e 100644 --- a/src/test/java/com/thealgorithms/ciphers/PlayfairTest.java +++ b/src/test/java/com/thealgorithms/ciphers/PlayfairTest.java @@ -34,4 +34,30 @@ public void testEncryptionAndDecryption() { assertEquals(plaintext, decryptedText); } + + @Test + public void testLowercaseKeyBehavesLikeUppercase() { + PlayfairCipher lowercase = new PlayfairCipher("monarchy"); + PlayfairCipher uppercase = new PlayfairCipher("MONARCHY"); + + assertEquals(uppercase.encrypt("HELLO"), lowercase.encrypt("HELLO")); + assertEquals("CFSUPM", lowercase.encrypt("HELLO")); + } + + @Test + public void testKeyContainingJStillCoversZ() { + PlayfairCipher playfairCipher = new PlayfairCipher("JUMP"); + + String decryptedText = playfairCipher.decrypt(playfairCipher.encrypt("ZOO")); + + assertEquals("ZOXO", decryptedText); + } + + @Test + public void testKeyWithNonLettersIsStripped() { + PlayfairCipher messy = new PlayfairCipher("Mon@rchy2026"); + PlayfairCipher clean = new PlayfairCipher("MONRCHY"); + + assertEquals(clean.encrypt("HELLO"), messy.encrypt("HELLO")); + } }