Skip to content

Instantly share code, notes, and snippets.

@vladaman
Created June 14, 2013 21:11
Show Gist options
  • Save vladaman/5785306 to your computer and use it in GitHub Desktop.
Save vladaman/5785306 to your computer and use it in GitHub Desktop.
Encrypt/Decrypt using AES. The key must be 128bits
private static String encrypt(String key, String data) throws Exception {
SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
byte[] res = cipher.doFinal(data.getBytes());
return Hex.encodeHexString(res);
}
private static String decrypt(String key, String d) throws Exception {
SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, keySpec);
return new String(cipher.doFinal(Hex.decodeHex(d.toCharArray())));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment