1
1
package com .thealgorithms .ciphers ;
2
2
3
3
/**
4
- * A Java implementation of Vigenere Cipher.
4
+ * A Java implementation of the Vigenère Cipher.
5
+ *
6
+ * The Vigenère Cipher is a polyalphabetic substitution cipher that uses a
7
+ * keyword to shift letters in the plaintext by different amounts, depending
8
+ * on the corresponding character in the keyword. It wraps around the alphabet,
9
+ * ensuring the shifts are within 'A'-'Z' or 'a'-'z'.
10
+ *
11
+ * Non-alphabetic characters (like spaces, punctuation) are kept unchanged.
12
+ *
13
+ * Encryption Example:
14
+ * - Plaintext: "Hello World!"
15
+ * - Key: "suchsecret"
16
+ * - Encrypted Text: "Zynsg Yfvev!"
17
+ *
18
+ * Decryption Example:
19
+ * - Ciphertext: "Zynsg Yfvev!"
20
+ * - Key: "suchsecret"
21
+ * - Decrypted Text: "Hello World!"
22
+ *
23
+ * Wikipedia Reference:
24
+ * <a href="https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher">Vigenère Cipher - Wikipedia</a>
5
25
*
6
26
* @author straiffix
7
27
* @author beingmartinbmc
8
28
*/
9
29
public class Vigenere {
10
30
31
+ /**
32
+ * Encrypts a given message using the Vigenère Cipher with the specified key.
33
+ * Steps:
34
+ * 1. Iterate over each character in the message.
35
+ * 2. If the character is a letter, shift it by the corresponding character in the key.
36
+ * 3. Preserve the case of the letter.
37
+ * 4. Preserve non-alphabetic characters.
38
+ * 5. Move to the next character in the key (cyclic).
39
+ * 6. Return the encrypted message.
40
+ *
41
+ * @param message The plaintext message to encrypt.
42
+ * @param key The keyword used for encryption.
43
+ * @throws IllegalArgumentException if the key is empty.
44
+ * @return The encrypted message.
45
+ */
11
46
public String encrypt (final String message , final String key ) {
12
- StringBuilder result = new StringBuilder ();
47
+ if (key .isEmpty ()) {
48
+ throw new IllegalArgumentException ("Key cannot be empty." );
49
+ }
13
50
51
+ StringBuilder result = new StringBuilder ();
14
52
int j = 0 ;
15
53
for (int i = 0 ; i < message .length (); i ++) {
16
54
char c = message .charAt (i );
@@ -20,17 +58,35 @@ public String encrypt(final String message, final String key) {
20
58
} else {
21
59
result .append ((char ) ((c + key .toLowerCase ().charAt (j ) - 2 * 'a' ) % 26 + 'a' ));
22
60
}
61
+ j = ++j % key .length ();
23
62
} else {
24
63
result .append (c );
25
64
}
26
- j = ++j % key .length ();
27
65
}
28
66
return result .toString ();
29
67
}
30
68
69
+ /**
70
+ * Decrypts a given message encrypted with the Vigenère Cipher using the specified key.
71
+ * Steps:
72
+ * 1. Iterate over each character in the message.
73
+ * 2. If the character is a letter, shift it back by the corresponding character in the key.
74
+ * 3. Preserve the case of the letter.
75
+ * 4. Preserve non-alphabetic characters.
76
+ * 5. Move to the next character in the key (cyclic).
77
+ * 6. Return the decrypted message.
78
+ *
79
+ * @param message The encrypted message to decrypt.
80
+ * @param key The keyword used for decryption.
81
+ * @throws IllegalArgumentException if the key is empty.
82
+ * @return The decrypted plaintext message.
83
+ */
31
84
public String decrypt (final String message , final String key ) {
32
- StringBuilder result = new StringBuilder ();
85
+ if (key .isEmpty ()) {
86
+ throw new IllegalArgumentException ("Key cannot be empty." );
87
+ }
33
88
89
+ StringBuilder result = new StringBuilder ();
34
90
int j = 0 ;
35
91
for (int i = 0 ; i < message .length (); i ++) {
36
92
char c = message .charAt (i );
@@ -40,11 +96,10 @@ public String decrypt(final String message, final String key) {
40
96
} else {
41
97
result .append ((char ) ('z' - (25 - (c - key .toLowerCase ().charAt (j ))) % 26 ));
42
98
}
99
+ j = ++j % key .length ();
43
100
} else {
44
101
result .append (c );
45
102
}
46
-
47
- j = ++j % key .length ();
48
103
}
49
104
return result .toString ();
50
105
}
0 commit comments