AES/CBC/PKCS5Padding decrypt - Android java.security

Android examples for java.security:AES

Description

AES/CBC/PKCS5Padding decrypt

Demo Code

import android.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class Main{

    private final static String DEFAULT_KEY = "ITAG_KEY_MANAGER";
    private final static byte[] iv = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B',
            'C', 'D', 'E', 'F', 0 };
    public static String decrypt(String src) throws Exception {
        return decrypt(DEFAULT_KEY, src);
    }/*from  w  ww.j ava2  s.  c o  m*/
    public static String decrypt(String key, String src) throws Exception {
        byte[] keyByte = key.getBytes();
        SecretKeySpec keySpec = new SecretKeySpec(keyByte, "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        IvParameterSpec ivSpec = new IvParameterSpec(iv);
        cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);

        byte[] srcByte = Base64.decode(src, Base64.DEFAULT);

        byte[] decrypted = cipher.doFinal(srcByte);
        String result = new String(decrypted);
        return result;
    }


}

Related Tutorials