|
2 | 2 |
|
3 | 3 | public final class MonoAlphabetic {
|
4 | 4 |
|
5 |
| - private final String key; |
6 |
| - |
7 |
| - // Constructor to initialize the key for encryption and decryption |
8 |
| - public MonoAlphabetic(String key) { |
9 |
| - this.key = key.toUpperCase(); // Store the key in uppercase to match the encryption/decryption logic |
| 5 | + // Private constructor to prevent instantiation of utility class |
| 6 | + private MonoAlphabetic() { |
| 7 | + throw new UnsupportedOperationException("Utility class"); |
10 | 8 | }
|
11 | 9 |
|
12 | 10 | // Encryption method
|
13 |
| - public String encrypt(String data) { |
14 |
| - StringBuilder sb = new StringBuilder(data.toUpperCase()); |
15 |
| - |
16 |
| - for (int i = 0; i < sb.length(); i++) { |
17 |
| - char currentChar = sb.charAt(i); |
18 |
| - if (Character.isLetter(currentChar)) { // Check if it's a letter |
19 |
| - int index = currentChar - 'A'; // Get the index for the character |
20 |
| - sb.setCharAt(i, key.charAt(index)); // Replace with the key character |
| 11 | + public static String encrypt(String data, String key) { |
| 12 | + StringBuilder sb = new StringBuilder(); |
| 13 | + |
| 14 | + // Convert to uppercase to match the key mapping |
| 15 | + data = data.toUpperCase(); |
| 16 | + |
| 17 | + for (char c : data.toCharArray()) { |
| 18 | + if (c >= 'A' && c <= 'Z') { |
| 19 | + // Get the index (0-25) for the character |
| 20 | + int idx = c - 'A'; |
| 21 | + // Append the character at the corresponding index in the key |
| 22 | + sb.append(key.charAt(idx)); |
| 23 | + } else { |
| 24 | + // If character is not A-Z, append it as is |
| 25 | + sb.append(c); |
21 | 26 | }
|
22 | 27 | }
|
23 | 28 | return sb.toString();
|
24 | 29 | }
|
25 | 30 |
|
26 | 31 | // Decryption method
|
27 |
| - public String decrypt(String data) { |
28 |
| - StringBuilder sb = new StringBuilder(data.toUpperCase()); |
29 |
| - |
30 |
| - for (int i = 0; i < sb.length(); i++) { |
31 |
| - char currentChar = sb.charAt(i); |
32 |
| - if (Character.isLetter(currentChar)) { // Check if it's a letter |
33 |
| - int index = key.indexOf(currentChar); // Find the character in the key |
34 |
| - if (index != -1) { |
35 |
| - sb.setCharAt(i, (char) (index + 'A')); // Replace with the original character |
| 32 | + public static String decrypt(String data, String key) { |
| 33 | + StringBuilder sb = new StringBuilder(); |
| 34 | + |
| 35 | + // Convert to uppercase to match the key mapping |
| 36 | + data = data.toUpperCase(); |
| 37 | + |
| 38 | + for (char c : data.toCharArray()) { |
| 39 | + if (c >= 'A' && c <= 'Z') { |
| 40 | + // Get the index from the key for the character |
| 41 | + int idx = getIndex(c, key); |
| 42 | + // Append the original character |
| 43 | + if (idx != -1) { |
| 44 | + char originalChar = (char) (idx + 'A'); |
| 45 | + sb.append(originalChar); |
36 | 46 | }
|
| 47 | + } else { |
| 48 | + // If character is not A-Z, append it as is |
| 49 | + sb.append(c); |
37 | 50 | }
|
38 | 51 | }
|
39 | 52 | return sb.toString();
|
40 | 53 | }
|
41 | 54 |
|
42 |
| - // Static utility methods for encryption/decryption without creating an instance |
43 |
| - public static String encrypt(String data, String key) { |
44 |
| - return new MonoAlphabetic(key).encrypt(data); |
45 |
| - } |
46 |
| - |
47 |
| - public static String decrypt(String data, String key) { |
48 |
| - return new MonoAlphabetic(key).decrypt(data); |
| 55 | + // Helper method to get index of a character in the key |
| 56 | + private static int getIndex(char c, String key) { |
| 57 | + for (int i = 0; i < key.length(); i++) { |
| 58 | + if (key.charAt(i) == c) { |
| 59 | + return i; // Return the index if the character matches |
| 60 | + } |
| 61 | + } |
| 62 | + return -1; // Return -1 if character not found (should not happen for valid inputs) |
49 | 63 | }
|
50 | 64 | }
|
0 commit comments