Skip to content

Commit 0159df9

Browse files
committed
Added ADFGVXCipherTest.java
1 parent 677cc08 commit 0159df9

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

src/main/java/com/thealgorithms/ciphers/ADFGVXCipher.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
*/
1616
public class ADFGVXCipher {
1717

18-
private static final char[] POLYBIUS_LETTERS = {'A', 'D', 'F', 'G', 'V', 'X'};
19-
private static final char[][] POLYBIUS_SQUARE = {{'P', 'H', '0', 'Q', 'G', '6'}, {'4', 'M', 'E', 'A', '1', 'Y'}, {'L', '2', 'N', 'O', 'F', 'D'}, {'X', 'K', 'R', '3', 'C', 'V'}, {'S', '5', 'Z', 'W', '7', 'B'}, {'J', '9', 'U', 'T', 'I', '8'}};
18+
private static final char[] POLYBIUS_LETTERS = {'A', 'D', 'F', 'G', 'V', 'X'};
19+
private static final char[][] POLYBIUS_SQUARE = {{'N', 'A', '1', 'C', '3', 'H'}, {'8', 'T', 'B', '2', 'O', 'M'}, {'E', '5', 'W', 'R', 'P', 'D'}, {'4', 'F', '6', 'G', '7', 'I'}, {'9', 'J', '0', 'K', 'L', 'Q'}, {'S', 'U', 'V', 'X', 'Y', 'Z'}};
2020
private static final Map<String, Character> POLYBIUS_MAP = new HashMap<>();
2121
private static final Map<Character, String> REVERSE_POLYBIUS_MAP = new HashMap<>();
2222

@@ -31,7 +31,7 @@ public class ADFGVXCipher {
3131
}
3232

3333
// Encrypts the plaintext using the ADFGVX cipher
34-
public String encrypt(String plaintext, String key) {
34+
private String encrypt(String plaintext, String key) {
3535
plaintext = plaintext.toUpperCase().replaceAll("[^A-Z0-9]", "");
3636
StringBuilder fractionatedText = new StringBuilder();
3737

@@ -45,7 +45,7 @@ public String encrypt(String plaintext, String key) {
4545
}
4646

4747
// Decrypts the ciphertext using the ADFGVX cipher
48-
public String decrypt(String ciphertext, String key) {
48+
private String decrypt(String ciphertext, String key) {
4949
// Step 1: Reverse the columnar transposition
5050
String fractionatedText = reverseColumnarTransposition(ciphertext, key);
5151

@@ -120,4 +120,4 @@ private String reverseColumnarTransposition(String ciphertext, String key) {
120120

121121
return fractionatedText.toString();
122122
}
123-
}
123+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.thealgorithms.ciphers;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class ADFGVXCipher {
8+
9+
ADFGVXCipher adfgvxCipher = new ADFGVXCipher();
10+
11+
@Test
12+
void adfgvxCipherEncryptTest() {
13+
// given
14+
String message = "attack at 1200am"; // Plaintext message
15+
String keyword = "PRIVACY";
16+
17+
// when
18+
String cipherText = hillCipher.encrypt(message, keyword);
19+
20+
// then
21+
assertEquals("DGDDDAGDDGAFADDFDADVDVFAADVX", cipherText);
22+
}
23+
24+
@Test
25+
void adfgvxCipherDecryptTest() {
26+
// given
27+
String cipherText = "DGDDDAGDDGAFADDFDADVDVFAADVX"; // Ciphertext message
28+
String keyword = "PRIVACY";
29+
30+
// when
31+
String plainText = hillCipher.decrypt(cipherText, keyword);
32+
33+
// then
34+
assertEquals("ATTACKAT1200AM", plainText);
35+
}
36+
}

0 commit comments

Comments
 (0)