Skip to content

Instantly share code, notes, and snippets.

@anitaa1990
Created April 6, 2018 09:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anitaa1990/5229cbf2ed7df993a6f2480b978c5f69 to your computer and use it in GitHub Desktop.
Save anitaa1990/5229cbf2ed7df993a6f2480b978c5f69 to your computer and use it in GitHub Desktop.
AES Encryption - Android
public class AES {
public static String encrypt(String key, String algorithm, String value) {
try {
SecretKey secretKey = new SecretKeySpec(Base64.decode(key.getBytes(), Base64.NO_WRAP), "AES");
AlgorithmParameterSpec iv = new IvParameterSpec(Base64.decode(key.getBytes(), Base64.NO_WRAP));
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
return new String(Base64.encode(cipher.doFinal(value.getBytes("UTF-8")), Base64.NO_WRAP));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
//Usage:
String key = "zjcSX3TumLzbJfpW\\/Zzung==";
String padding = "AES/CBC/PKCS5Padding";
String value = "anitaaiostest1"
String encryptedString = encrypt(key, padding, value);
System.out.println(encryptedString) // -> uKpbnE5XviQFU+dpWWD0oA==
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment