AES Encrypiton and Decryption using java
public static void encryptOrDecrypt(String text) throws Exception {
try {
String key = "Bar12345Bar12345"; // 128 bit key
Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, aesKey);
byte[] encrypted = cipher.doFinal(text.getBytes());
System.out.println(new String(encrypted));
cipher.init(Cipher.DECRYPT_MODE, aesKey);
String decrypted = new String(cipher.doFinal(encrypted));
System.out.println(decrypted);
} catch (Exception e) {
e.printStackTrace();
}
}
try {
String key = "Bar12345Bar12345"; // 128 bit key
Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, aesKey);
byte[] encrypted = cipher.doFinal(text.getBytes());
System.out.println(new String(encrypted));
cipher.init(Cipher.DECRYPT_MODE, aesKey);
String decrypted = new String(cipher.doFinal(encrypted));
System.out.println(decrypted);
} catch (Exception e) {
e.printStackTrace();
}
}
Post a Comment
0 Comments