Java program to encipher and decipher text using Vigen?re Cipher.
Text: ATTACKATDAWN Key: LEMONLEMONLE Enciphered: LXFOPVEFRNHR Deciphered: ATTACKATDAWN
// ? 2019 TheFlyingKeyboard and released under MIT License // theflyingkeyboard.net public class Main { public static void main(String[] args) { String text = "ATTACKATDAWN"; String key = "LEMONLEMONLE"; System.out.println("Text: " + text); System.out.println("Key: " + key); String ciphertext = encipher(text, key); System.out.println("Enciphered: " + ciphertext); System.out.println("Deciphered: " + decipher(ciphertext, key)); } private static String encipher(String text, String key) { String result = ""; text = text.toUpperCase(); for (int i = 0; i < text.length(); ++i) { result += (char) ((((text.charAt(i) - 'A') + (key.charAt(i) - 'A')) % 26) + 'A'); } return result; } private static String decipher(String text, String key) { String subtractedKey = ""; for (int i = 0; i < key.length(); ++i) { subtractedKey += (char) (('Z' - key.charAt(i) + 1) + 'A'); } return encipher(text, subtractedKey); } }
Java Vigen?re Cipher