import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import java.util.Base64; import java.util.Scanner; public class AESEncryption { // Encrypt text using AES algorithm public static String encrypt(String plainText, SecretKey key) throws Exception { Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] encryptedBytes = cipher.doFinal(plainText.getBytes()); return Base64.getEncoder().encodeToString(encryptedBytes); } // Decrypt text using AES algorithm public static String decrypt(String encryptedText, SecretKey key) throws Exception { Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, key); byte[] decodedBytes = Base64.getDecoder().decode(encryptedText); byte[] decryptedBytes = cipher.doFinal(decodedBytes); return new String(decryptedBytes); } public static void main(String[] args) throws Exception { Scanner scanner = new Scanner(System.in); System.out.println("=== AES Encryption & Decryption Demo ==="); System.out.print("Enter text to encrypt: "); String text = scanner.nextLine(); // Generate AES key (128-bit) KeyGenerator keyGen = KeyGenerator.getInstance("AES"); keyGen.init(128); SecretKey secretKey = keyGen.generateKey(); // Encrypt String encryptedText = encrypt(text, secretKey); System.out.println("Encrypted text: " + encryptedText); // Decrypt String decryptedText = decrypt(encryptedText, secretKey); System.out.println("Decrypted text: " + decryptedText); // Print key for reference System.out.println("Secret Key (Base64): " + Base64.getEncoder().encodeToString(secretKey.getEncoded())); scanner.close(); } }