import android.text.TextUtils;
import android.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class AesUtils {
public static String Encrypt(String text) {
if (TextUtils.isEmpty(text)) {
return text;
}
try {
byte[] result = encrypt("your key", text);
return new String(Base64.encode(result, Base64.DEFAULT));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String Decrypt(String text) {
try {
byte[] result = decrypt(Base64.decode(text, Base64.DEFAULT), "your key")
return new String(result);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static byte[] encrypt(String key, String text) throws Exception {
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
return cipher.doFinal(text.getBytes("UTF-8"));
}
private static byte[] decrypt(byte[] content, String key) throws Exception {
SecretKeySpec key = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.DECRYPT_MODE, key);
return cipher.doFinal(content);
}
}