File tree 1 file changed +5
-9
lines changed
src/main/java/com/thealgorithms/ciphers 1 file changed +5
-9
lines changed Original file line number Diff line number Diff line change @@ -14,9 +14,6 @@ public static String encrypt(String data, String key) {
14
14
}
15
15
StringBuilder sb = new StringBuilder ();
16
16
17
- // Convert input data to uppercase
18
- data = data .toUpperCase ();
19
-
20
17
// Encrypt each character
21
18
for (char c : data .toCharArray ()) {
22
19
int idx = charToPos (c ); // Get the index in the alphabet
@@ -29,14 +26,13 @@ public static String encrypt(String data, String key) {
29
26
public static String decrypt (String data , String key ) {
30
27
StringBuilder sb = new StringBuilder ();
31
28
32
- // Convert input data to uppercase
33
- data = data .toUpperCase ();
34
-
35
29
// Decrypt each character
36
30
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
40
36
}
41
37
return sb .toString ();
42
38
}
You can’t perform that action at this time.
0 commit comments