AES Decrypt ECB - Android java.security

Android examples for java.security:AES

Description

AES Decrypt ECB

Demo Code

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

import android.annotation.SuppressLint;
import android.util.Base64;

public class Main {

  @SuppressLint("TrulyRandom")
  public static String DecryptECB(String stringToEncode, String Key) throws Exception {
    try {//from   w  w w. ja  v a  2  s.co  m
      if (Key == null) {
        return null;
      }
      byte[] raw = Key.getBytes("utf-8");
      SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
      Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
      cipher.init(Cipher.DECRYPT_MODE, skeySpec);
      byte[] encrypted1 = Base64.decode(stringToEncode, 0);
      try {
        byte[] original = cipher.doFinal(encrypted1);
        String originalString = new String(original, "utf-8");
        return originalString;
      } catch (Exception e) {
        System.out.println(e.toString());
        return null;
      }
    } catch (Exception ex) {
      System.out.println(ex.toString());
      return null;
    }
  }

}

Related Tutorials