2021-09-26 17:26:59 +08:00
|
|
|
package Ciphers;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A Java implementation of Vigenere Cipher.
|
|
|
|
*
|
|
|
|
* @author straiffix
|
|
|
|
* @author beingmartinbmc
|
|
|
|
*/
|
|
|
|
public class Vigenere {
|
|
|
|
|
2021-10-29 13:19:42 +08:00
|
|
|
public static String encrypt(final String message, final String key) {
|
|
|
|
|
|
|
|
StringBuilder result = new StringBuilder();
|
|
|
|
|
|
|
|
for (int i = 0, j = 0; i < message.length(); i++) {
|
|
|
|
char c = message.charAt(i);
|
|
|
|
if (Character.isLetter(c)) {
|
|
|
|
if (Character.isUpperCase(c)) {
|
|
|
|
result.append((char) ((c + key.toUpperCase().charAt(j) - 2 * 'A') % 26 + 'A'));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
result.append((char) ((c + key.toLowerCase().charAt(j) - 2 * 'a') % 26 + 'a'));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result.append(c);
|
|
|
|
}
|
|
|
|
j = ++j % key.length();
|
2021-09-26 17:26:59 +08:00
|
|
|
}
|
2021-10-29 13:19:42 +08:00
|
|
|
return result.toString();
|
2021-09-26 17:26:59 +08:00
|
|
|
}
|
|
|
|
|
2021-10-29 13:19:42 +08:00
|
|
|
public static String decrypt(final String message, final String key) {
|
|
|
|
StringBuilder result = new StringBuilder();
|
2021-09-26 17:26:59 +08:00
|
|
|
|
2021-10-29 13:19:42 +08:00
|
|
|
for (int i = 0, j = 0; i < message.length(); i++) {
|
2021-09-26 17:26:59 +08:00
|
|
|
|
2021-10-29 13:19:42 +08:00
|
|
|
char c = message.charAt(i);
|
|
|
|
if (Character.isLetter(c)) {
|
|
|
|
if (Character.isUpperCase(c)) {
|
|
|
|
result.append((char) ('Z' - (25 - (c - key.toUpperCase().charAt(j))) % 26));
|
2021-09-26 17:26:59 +08:00
|
|
|
|
2021-10-29 13:19:42 +08:00
|
|
|
} else {
|
|
|
|
result.append((char) ('z' - (25 - (c - key.toLowerCase().charAt(j))) % 26));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result.append(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
j = ++j % key.length();
|
2021-09-26 17:26:59 +08:00
|
|
|
}
|
2021-10-29 13:19:42 +08:00
|
|
|
return result.toString();
|
|
|
|
}
|
2021-09-26 17:26:59 +08:00
|
|
|
|
2021-10-29 13:19:42 +08:00
|
|
|
public static void main(String[] args) {
|
|
|
|
String text = "Hello World!";
|
|
|
|
String key = "itsakey";
|
|
|
|
System.out.println(text);
|
|
|
|
String ciphertext = encrypt(text, key);
|
|
|
|
System.out.println(ciphertext);
|
|
|
|
System.out.println(decrypt(ciphertext, key));
|
2021-09-26 17:26:59 +08:00
|
|
|
}
|
|
|
|
}
|