private static String algorithm = "DES";
private static Key key = null;
private static Cipher cipher = null;
private static void setUp() throws Exception {
key = KeyGenerator.getInstance(algorithm).generateKey();
cipher = Cipher.getInstance(algorithm); }
public static void main(String[] args) throws Exception {
setUp(); byte[] encryptionBytes = null;
String input = "tiagopereira86@gmail.com";
System.out.println("Entered: " + input);
encryptionBytes = encrypt(input);
String s = new sun.misc.BASE64Encoder().encode(encryptionBytes);
System.out.println(s);
encryptionBytes = new sun.misc.BASE64Decoder().decodeBuffer(s);
System.out.println("Recovered: " + decrypt(encryptionBytes));
}
private static byte[] encrypt(String input) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] inputBytes = input.getBytes();
return cipher.doFinal(inputBytes);
}
private static String decrypt(byte[] encryptionBytes) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] recoveredBytes = cipher.doFinal(encryptionBytes);
String recovered = new String(recoveredBytes); return recovered;
}
No responses yet for "Encrypt Decrypt using AES"
Post a Comment