decrypt Base64 String - Android java.lang

Android examples for java.lang:String Base64

Description

decrypt Base64 String

Demo Code


//package com.java2s;

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

public class Main {
    private static final String UTF8 = "UTF-8";

    static String decryptBase64(String base64EncodedCryptedText,
            String encryptKey, String encryptIv) {
        byte[] byteArray = Base64.decode(base64EncodedCryptedText,
                Base64.DEFAULT);//w w  w  .j av  a 2  s  .  c  om
        String ret = decrypt(byteArray, encryptKey, encryptIv);
        return ret;
    }

    static String decrypt(byte[] byteArray, String encryptKey,
            String encryptIv) {

        if (byteArray == null) {
            return null;
        }

        String strResult = null;

        try {
            byte[] byteKey = encryptKey.getBytes(UTF8);
            byte[] byteIv = encryptIv.getBytes(UTF8);
            SecretKeySpec key = new SecretKeySpec(byteKey, "AES");
            IvParameterSpec iv = new IvParameterSpec(byteIv);
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, key, iv);
            byte[] byteResult = cipher.doFinal(byteArray);

            strResult = new String(byteResult, UTF8);

        } catch (Exception e) {
        }

        return strResult;
    }
}

Related Tutorials