Skip to content

Commit 736ec03

Browse files
authored
Update MonoAlphabetic.java
1 parent 8ffc13b commit 736ec03

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ public static String encrypt(String data, String key) {
1414
}
1515
StringBuilder sb = new StringBuilder();
1616

17-
// Convert input data to uppercase
18-
data = data.toUpperCase();
19-
2017
// Encrypt each character
2118
for (char c : data.toCharArray()) {
2219
int idx = charToPos(c); // Get the index in the alphabet
@@ -29,14 +26,13 @@ public static String encrypt(String data, String key) {
2926
public static String decrypt(String data, String key) {
3027
StringBuilder sb = new StringBuilder();
3128

32-
// Convert input data to uppercase
33-
data = data.toUpperCase();
34-
3529
// Decrypt each character
3630
for (char c : data.toCharArray()) {
37-
int idx = charToPos(c); // Get the index in the key
38-
char decryptedChar = posToChar(key.indexOf(c)); // Find the position in the key and convert back to char
39-
sb.append(decryptedChar); // Append the decrypted character
31+
int idx = key.indexOf(c); // Find the index in the key
32+
if (idx == -1) {
33+
throw new IllegalArgumentException("Input data contains invalid characters.");
34+
}
35+
sb.append(posToChar(idx)); // Convert the index back to the original character
4036
}
4137
return sb.toString();
4238
}

0 commit comments

Comments
 (0)