Skip to content

Commit 2cedfc8

Browse files
authored
Merge branch 'master' into fix-median-overflow
2 parents 325f145 + c86ec0a commit 2cedfc8

4 files changed

Lines changed: 65 additions & 5 deletions

File tree

‎src/main/java/com/thealgorithms/ciphers/HillCipher.java‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,22 @@ public String decrypt(String message, int[][] inverseKeyMatrix) {
6868
return plainText.toString();
6969
}
7070

71-
// Validates that the determinant of the key matrix is not zero modulo 26
71+
// Validates that the key matrix is invertible modulo 26, which requires the
72+
// determinant to be coprime with 26. Checking only for a zero determinant is not
73+
// enough: a determinant of 2 or 13 (or any other non-unit) passes that check but
74+
// has no inverse, so encryption collapses distinct plaintexts onto the same
75+
// ciphertext and decryption becomes impossible.
7276
private void validateDeterminant(int[][] keyMatrix, int n) {
73-
int det = determinant(keyMatrix, n) % 26;
74-
if (det == 0) {
75-
throw new IllegalArgumentException("Invalid key matrix. Determinant is zero modulo 26.");
77+
int det = Math.floorMod(determinant(keyMatrix, n), 26);
78+
if (gcd(det, 26) != 1) {
79+
throw new IllegalArgumentException("Invalid key matrix. Determinant must be coprime with 26 to be invertible modulo 26.");
7680
}
7781
}
7882

83+
private int gcd(int a, int b) {
84+
return b == 0 ? a : gcd(b, a % b);
85+
}
86+
7987
// Computes the determinant of a matrix recursively
8088
private int determinant(int[][] matrix, int n) {
8189
int det = 0;

‎src/main/java/com/thealgorithms/ciphers/PlayfairCipher.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ public class PlayfairCipher {
66
private String key;
77

88
public PlayfairCipher(String key) {
9-
this.key = key;
9+
// Normalize the key the same way the plaintext is normalized in encrypt():
10+
// uppercase, J folded into I, and non-letters removed. Without this a
11+
// lowercase or mixed-case key, a key containing J, or a key with
12+
// punctuation silently produces a wrong matrix.
13+
this.key = key.toUpperCase().replace("J", "I").replaceAll("[^A-Z]", "");
1014
generateMatrix();
1115
}
1216

‎src/test/java/com/thealgorithms/ciphers/HillCipherTest.java‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.thealgorithms.ciphers;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
45

56
import org.junit.jupiter.api.Test;
67

@@ -33,4 +34,25 @@ void hillCipherDecryptTest() {
3334
// then
3435
assertEquals("ACT", plainText);
3536
}
37+
38+
@Test
39+
void rejectsNonInvertibleKeyWithEvenDeterminant() {
40+
// det = 2, gcd(2, 26) = 2: passes the old zero-check but has no inverse mod 26
41+
int[][] keyMatrix = {{1, 0}, {0, 2}};
42+
assertThrows(IllegalArgumentException.class, () -> hillCipher.encrypt("AA", keyMatrix));
43+
}
44+
45+
@Test
46+
void rejectsNonInvertibleKeyWithDeterminantThirteen() {
47+
// det = 13, gcd(13, 26) = 13: also not invertible mod 26
48+
int[][] keyMatrix = {{13, 0}, {0, 1}};
49+
assertThrows(IllegalArgumentException.class, () -> hillCipher.encrypt("BB", keyMatrix));
50+
}
51+
52+
@Test
53+
void acceptsKeyWithNegativeCoprimeDeterminant() {
54+
// det = -1, coprime with 26: a valid key that a naive signed modulo would misclassify
55+
int[][] keyMatrix = {{1, 2}, {1, 1}};
56+
assertEquals("XP", hillCipher.encrypt("HI", keyMatrix));
57+
}
3658
}

‎src/test/java/com/thealgorithms/ciphers/PlayfairTest.java‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,30 @@ public void testEncryptionAndDecryption() {
3434

3535
assertEquals(plaintext, decryptedText);
3636
}
37+
38+
@Test
39+
public void testLowercaseKeyBehavesLikeUppercase() {
40+
PlayfairCipher lowercase = new PlayfairCipher("monarchy");
41+
PlayfairCipher uppercase = new PlayfairCipher("MONARCHY");
42+
43+
assertEquals(uppercase.encrypt("HELLO"), lowercase.encrypt("HELLO"));
44+
assertEquals("CFSUPM", lowercase.encrypt("HELLO"));
45+
}
46+
47+
@Test
48+
public void testKeyContainingJStillCoversZ() {
49+
PlayfairCipher playfairCipher = new PlayfairCipher("JUMP");
50+
51+
String decryptedText = playfairCipher.decrypt(playfairCipher.encrypt("ZOO"));
52+
53+
assertEquals("ZOXO", decryptedText);
54+
}
55+
56+
@Test
57+
public void testKeyWithNonLettersIsStripped() {
58+
PlayfairCipher messy = new PlayfairCipher("Mon@rchy2026");
59+
PlayfairCipher clean = new PlayfairCipher("MONRCHY");
60+
61+
assertEquals(clean.encrypt("HELLO"), messy.encrypt("HELLO"));
62+
}
3763
}

0 commit comments

Comments
 (0)