Import Android.annotation.suppresslint;import Java.security.securerandom;import Javax.crypto.cipher;import Javax.crypto.keygenerator;import Javax.crypto.secretkey;import Javax.crypto.spec.ivparameterspec;import javax.crypto.spec.secretkeyspec;/** * * * Author:sunger */public class Aesutils {public static string encrypt (string see D, String cleartext) throws Exception {byte[] Rawkey = Getrawkey (Seed.getbytes ()); byte[] result = Encrypt (Rawkey, Cleartex T.getbytes ()); return Tohex (result);} public static string decrypt (string seed, string encrypted) throws Exception {byte[] Rawkey = Getrawkey (Seed.getbytes ()); b yte[] enc = tobyte (encrypted); byte[] result = Decrypt (rawkey, ENC); return new String (result); @SuppressLint ("Trulyrandom") private static byte[] Getrawkey (byte[] seed) throws Exception {Keygenerator KGen = Keygenerator.getinstance ("AES"); securerandom sr = securerandom.getinstance ("Sha1prng", "Crypto"); Sr.setseed (seed); Kgen.init (SR); 192 and AvailablesecretkeySkey = Kgen.generatekey (); byte[] raw = skey.getencoded (); return raw;} private static byte[] Encrypt (byte[] raw, byte[] clear) throws Exception {Secretkeyspec Skeyspec = new Secretkeyspec (Raw, "AES"); Cipher Cipher = cipher.getinstance ("AES"); Cipher.init (Cipher.encrypt_mode, Skeyspec, New Ivparameterspec (New byte[ Cipher.getblocksize ()]); byte[] encrypted = cipher.dofinal (clear); return encrypted;} private static byte[] Decrypt (byte[] raw, byte[] encrypted) throws Exception {Secretkeyspec Skeyspec = new Secretkeyspec (RA W, "AES"); Cipher Cipher = cipher.getinstance ("AES"); Cipher.init (Cipher.decrypt_mode, Skeyspec, New Ivparameterspec (New byte[ Cipher.getblocksize ()])); byte[] decrypted = cipher.dofinal (encrypted); return decrypted;} private static byte[] ToByte (String hexstring) {int len = hexstring.length ()/2;byte[] result = new Byte[len];for (int i = 0; i < Len; i++) Result[i] = integer.valueof (hexstring.substring (2 * I, 2 * i + 2), +). Bytevalue (); return result;} private static String Tohex (byTe[] buf) {if (buf = = null) return ""; StringBuffer result = new StringBuffer (2 * buf.length); for (int i = 0; i < buf.length; i++) {Appendhex (result, buf[i]); }return result.tostring ();} Private final static String hex = "0123456789ABCDEF";p rivate static void Appendhex (StringBuffer sb, byte b) {Sb.append (HEX . CharAt ((b >> 4) & 0x0f). Append (Hex.charat (b & 0x0f));}}
Android AES Encryption tool class (measurement compatible with all versions, reliable)