package securecommunications; import java.security.*; import java.security.spec.InvalidKeySpecException; import javax.crypto.*; import javax.crypto.spec.SecretKeySpec; import sun.misc.*; public class Encryption { private static final String ALGO = "AES"; private static final byte[] keyValue = new byte[] { 'M', 'i', 's', 'c', 'p', '@', 's', 'S', 'w', '0', 'r','d', '2', '0', '1', '4' }; public static String encrypt(String Data) throws Exception { Key key = generateKey(); Cipher c = Cipher.getInstance(ALGO); c.init(Cipher.ENCRYPT_MODE, key); byte[] encVal = c.doFinal(Data.getBytes()); String encryptedValue = new BASE64Encoder().encode(encVal); return encryptedValue; } public static String decrypt(String encryptedData) throws Exception { Key key = generateKey(); Cipher c = Cipher.getInstance(ALGO); c.init(Cipher.DECRYPT_MODE, key); byte[] decodedValue = new BASE64Decoder().decodeBuffer(encryptedData); byte[] decValue = c.doFinal(decodedValue); String decryptedValue = new String(decValue); return decryptedValue; } private static Key generateKey() throws Exception { Key key = new SecretKeySpec(keyValue, ALGO); return key; } }