AES decode String - Android java.security

Android examples for java.security:AES

Description

AES decode String

Demo Code


import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import android.content.Context;

public class Main{
    private static final String ALGORISM = "AES/ECB/PKCS5Padding";
    private static Logger logger = Logger.getLogger(CryptoUtil.class
            .getPackage().getName());//w w w . j a v  a  2s.c  o m
    public static String decodeString(final String source,
            final SecretKeySpec keySpec) {
        logger.setLevel(Level.WARNING);
        String decoded = null;
        try {
            Cipher cipher = Cipher.getInstance(ALGORISM);
            cipher.init(Cipher.DECRYPT_MODE, keySpec);
            decoded = new String(cipher.doFinal(StringUtil
                    .decodeBase64(source)));
        } catch (Exception e) {
            logger.log(Level.SEVERE, "CryptoUtil#decodeString exception: "
                    + e.getLocalizedMessage());
        }
        return decoded;
    }
}

Related Tutorials