Repository navigation
Expand file tree
/
Copy pathHillCipherTest.java
More file actions
58 lines (45 loc) · 1.84 KB
/
Copy pathHillCipherTest.java
File metadata and controls
58 lines (45 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.thealgorithms.ciphers;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
class HillCipherTest {
HillCipher hillCipher = new HillCipher();
@Test
void hillCipherEncryptTest() {
// given
String message = "ACT"; // Plaintext message
int[][] keyMatrix = {{6, 24, 1}, {13, 16, 10}, {20, 17, 15}}; // Encryption key matrix
// when
String cipherText = hillCipher.encrypt(message, keyMatrix);
// then
assertEquals("POH", cipherText);
}
@Test
void hillCipherDecryptTest() {
// given
String cipherText = "POH"; // Ciphertext message
int[][] inverseKeyMatrix = {{8, 5, 10}, {21, 8, 21}, {21, 12, 8}}; // Decryption (inverse key) matrix
// when
String plainText = hillCipher.decrypt(cipherText, inverseKeyMatrix);
// then
assertEquals("ACT", plainText);
}
@Test
void rejectsNonInvertibleKeyWithEvenDeterminant() {
// det = 2, gcd(2, 26) = 2: passes the old zero-check but has no inverse mod 26
int[][] keyMatrix = {{1, 0}, {0, 2}};
assertThrows(IllegalArgumentException.class, () -> hillCipher.encrypt("AA", keyMatrix));
}
@Test
void rejectsNonInvertibleKeyWithDeterminantThirteen() {
// det = 13, gcd(13, 26) = 13: also not invertible mod 26
int[][] keyMatrix = {{13, 0}, {0, 1}};
assertThrows(IllegalArgumentException.class, () -> hillCipher.encrypt("BB", keyMatrix));
}
@Test
void acceptsKeyWithNegativeCoprimeDeterminant() {
// det = -1, coprime with 26: a valid key that a naive signed modulo would misclassify
int[][] keyMatrix = {{1, 2}, {1, 1}};
assertEquals("XP", hillCipher.encrypt("HI", keyMatrix));
}
}