Created
April 6, 2018 09:06
-
-
Save anitaa1990/5229cbf2ed7df993a6f2480b978c5f69 to your computer and use it in GitHub Desktop.
AES Encryption - Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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