AES Decrypt byte array by string key - Android java.security

Android examples for java.security:AES

Description

AES Decrypt byte array by string key

Demo Code

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import android.util.Base64;

public class Main {

  public static byte[] Encrypt(byte[] data, String key) throws Exception {
    try {//from   ww w.j  a  va 2s  .  c  o  m
      Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
      byte[] base64Key = Base64.decode(key, Base64.DEFAULT);
      SecretKeySpec keyspec = new SecretKeySpec(base64Key, "AES");
      cipher.init(Cipher.DECRYPT_MODE, keyspec);
      byte[] original = cipher.doFinal(data);
      return original;
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }

}

Related Tutorials