|
| 1 | +package com.thealgorithms.ciphers; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | + |
| 5 | +import java.util.stream.Stream; |
| 6 | + |
| 7 | +import org.junit.jupiter.params.ParameterizedTest; |
| 8 | +import org.junit.jupiter.params.provider.Arguments; |
| 9 | +import org.junit.jupiter.params.provider.MethodSource; |
| 10 | + |
1 | 11 | public final class MonoAlphabetic {
|
| 12 | + |
| 13 | + // Private constructor to prevent instantiation of utility class |
2 | 14 | private MonoAlphabetic() {
|
3 | 15 | throw new UnsupportedOperationException("Utility class");
|
4 | 16 | }
|
5 | 17 |
|
| 18 | + // Encryption method |
6 | 19 | public static String encrypt(String data, String key) {
|
7 | 20 | int idx;
|
8 | 21 | char c;
|
9 |
| - StringBuffer sb = new StringBuffer(data.toUpperCase()); |
| 22 | + StringBuilder sb = new StringBuilder(data.toUpperCase()); |
10 | 23 |
|
11 | 24 | for (int i = 0; i < sb.length(); i++) {
|
12 |
| - idx = sb.charAt(i) - 65; |
13 |
| - c = key.charAt(idx); |
14 |
| - sb.setCharAt(i, c); |
| 25 | + idx = sb.charAt(i) - 65; // Subtract ASCII value of 'A' to get index |
| 26 | + c = key.charAt(idx); // Find the character at the corresponding key position |
| 27 | + sb.setCharAt(i, c); // Replace with the key character |
15 | 28 | }
|
16 |
| - return new String(sb); |
| 29 | + return sb.toString(); |
17 | 30 | }
|
18 | 31 |
|
| 32 | + // Decryption method |
19 | 33 | public static String decrypt(String data, String key) {
|
20 | 34 | int idx;
|
21 | 35 | char c;
|
22 |
| - StringBuffer sb = new StringBuffer(data.toUpperCase()); |
| 36 | + StringBuilder sb = new StringBuilder(data.toUpperCase()); |
23 | 37 |
|
24 | 38 | for (int i = 0; i < sb.length(); i++) {
|
25 |
| - c = sb.charAt(i); |
26 |
| - idx = getIndex(c, key); |
27 |
| - c = (char) (idx + 65); |
28 |
| - sb.setCharAt(i, c); |
| 39 | + c = sb.charAt(i); // Get the character from encrypted data |
| 40 | + idx = getIndex(c, key); // Get the corresponding index from the key |
| 41 | + c = (char) (idx + 65); // Convert index back to character |
| 42 | + sb.setCharAt(i, c); // Replace with the original character |
29 | 43 | }
|
30 |
| - return new String(sb); |
| 44 | + return sb.toString(); |
31 | 45 | }
|
32 | 46 |
|
| 47 | + // Helper method to get index of a character in the key |
33 | 48 | public static int getIndex(char c, String key) {
|
34 | 49 | for (int i = 0; i < key.length(); i++) {
|
35 | 50 | if (key.charAt(i) == c) {
|
36 |
| - return i; |
| 51 | + return i; // Return the index if the character matches |
37 | 52 | }
|
38 | 53 | }
|
39 |
| - return -1; |
| 54 | + return -1; // Return -1 if character not found (should not happen for valid inputs) |
40 | 55 | }
|
41 |
| -} |
42 |
| - |
43 |
| -// JUnit Tests for MonoAlphabetic Cipher |
44 |
| -import org.junit.jupiter.api.DisplayName; |
45 |
| -import org.junit.jupiter.params.ParameterizedTest; |
46 |
| -import org.junit.jupiter.params.provider.CsvSource; |
47 |
| -import static org.junit.jupiter.api.Assertions.*; |
48 | 56 |
|
49 |
| -public class MonoAlphabeticTest { |
| 57 | + // *********** Unit Test Section ************** |
50 | 58 |
|
51 |
| - private final String key = "MNBVCXZLKJHGFDSAPOIUYTREWQ"; |
| 59 | + // Method to provide test data for encryption |
| 60 | + private static Stream<Arguments> provideEncryptionData() { |
| 61 | + String key = "MNBVCXZLKJHGFDSAPOIUYTREWQ"; |
| 62 | + return Stream.of( |
| 63 | + // Input data, key, expected encrypted output |
| 64 | + Arguments.of("HELLO", key, "GFSSD"), |
| 65 | + Arguments.of("JAVA", key, "MZSM") |
| 66 | + ); |
| 67 | + } |
52 | 68 |
|
| 69 | + // Test for encryption |
53 | 70 | @ParameterizedTest
|
54 |
| - @DisplayName("Encrypt Test with MonoAlphabetic Cipher") |
55 |
| - @CsvSource({ |
56 |
| - "HELLO, DLZZI", |
57 |
| - "WORLD, XMFLD", |
58 |
| - "JAVA, HBWB", |
59 |
| - "OPENAI, IUPNMW" |
60 |
| - }) |
61 |
| - void testEncrypt(String plainText, String expectedCipherText) { |
62 |
| - String encrypted = MonoAlphabetic.encrypt(plainText, key); |
63 |
| - assertEquals(expectedCipherText, encrypted, "Encryption failed for input: " + plainText); |
| 71 | + @MethodSource("provideEncryptionData") |
| 72 | + public void testEncrypt(String data, String key, String expected) { |
| 73 | + assertEquals(expected, MonoAlphabetic.encrypt(data, key)); |
| 74 | + } |
| 75 | + |
| 76 | + // Method to provide test data for decryption |
| 77 | + private static Stream<Arguments> provideDecryptionData() { |
| 78 | + String key = "MNBVCXZLKJHGFDSAPOIUYTREWQ"; |
| 79 | + return Stream.of( |
| 80 | + // Encrypted data, key, expected decrypted output |
| 81 | + Arguments.of("GFSSD", key, "HELLO"), |
| 82 | + Arguments.of("MZSM", key, "JAVA") |
| 83 | + ); |
64 | 84 | }
|
65 | 85 |
|
| 86 | + // Test for decryption |
66 | 87 | @ParameterizedTest
|
67 |
| - @DisplayName("Decrypt Test with MonoAlphabetic Cipher") |
68 |
| - @CsvSource({ |
69 |
| - "DLZZI, HELLO", |
70 |
| - "XMFLD, WORLD", |
71 |
| - "HBWB, JAVA", |
72 |
| - "IUPNMW, OPENAI" |
73 |
| - }) |
74 |
| - void testDecrypt(String cipherText, String expectedPlainText) { |
75 |
| - String decrypted = MonoAlphabetic.decrypt(cipherText, key); |
76 |
| - assertEquals(expectedPlainText, decrypted, "Decryption failed for input: " + cipherText); |
| 88 | + @MethodSource("provideDecryptionData") |
| 89 | + public void testDecrypt(String data, String key, String expected) { |
| 90 | + assertEquals(expected, MonoAlphabetic.decrypt(data, key)); |
77 | 91 | }
|
78 | 92 | }
|
0 commit comments