import java.util.Scanner; public class VigenereCipher { public static String encrypt(String text, String key) { StringBuilder result = new StringBuilder(); key = key.toUpperCase(); int keyIndex = 0; for (char ch : text.toCharArray()) { if (Character.isLetter(ch)) { char base = Character.isUpperCase(ch) ? 'A' : 'a'; int shift = key.charAt(keyIndex % key.length()) - 'A'; char encrypted = (char) ((ch - base + shift) % 26 + base); result.append(encrypted); keyIndex++; } else { result.append(ch); } } return result.toString(); } public static String decrypt(String text, String key) { StringBuilder result = new StringBuilder(); key = key.toUpperCase(); int keyIndex = 0; for (char ch : text.toCharArray()) { if (Character.isLetter(ch)) { char base = Character.isUpperCase(ch) ? 'A' : 'a'; int shift = key.charAt(keyIndex % key.length()) - 'A'; char decrypted = (char) ((ch - base - shift + 26) % 26 + base); result.append(decrypted); keyIndex++; } else { result.append(ch); } } return result.toString(); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter message: "); String message = sc.nextLine(); System.out.print("Enter key: "); String key = sc.nextLine(); String encrypted = encrypt(message, key); String decrypted = decrypt(encrypted, key); System.out.println("Encrypted: " + encrypted); System.out.println("Decrypted: " + decrypted); sc.close(); } }