Skip to content

Commit c86ec0a

Browse files
Aditya2375alxkm
andauthored
fix: reject Hill cipher keys whose determinant is not coprime with 26 (#7780)
* fix: reject Hill cipher keys whose determinant is not coprime with 26 The old validation only rejected a determinant of zero modulo 26. A key matrix is invertible mod 26 only when gcd(det, 26) = 1, so keys with determinant 2, 13, etc. passed validation but made encryption lossy: with key [[1,0],[0,2]] both "AA" and "AN" encrypt to "AA". Check coprimality instead, using floorMod so negative determinants are classified correctly. * test: cover Hill cipher keys with determinant 2, 13, or negative coprime Regression tests for the key-validation fix: keys with determinant 2 or 13 must be rejected even though they are not zero mod 26, and a key with determinant -1 (coprime) must still be accepted. --------- Co-authored-by: Oleksandr <19151554+alxkm@users.noreply.github.com>
1 parent 9870de2 commit c86ec0a

2 files changed

Lines changed: 34 additions & 4 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/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
}

0 commit comments

Comments
 (0)