AES encode String - Android java.security

Android examples for java.security:AES

Description

AES encode 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());//from   ww  w .jav a  2  s  .c o  m
    public static String encodeString(final String source,
            final SecretKeySpec keySpec) {
        logger.setLevel(Level.WARNING);
        String encoded = null;
        try {
            Cipher cipher = Cipher.getInstance(ALGORISM);
            cipher.init(Cipher.ENCRYPT_MODE, keySpec);
            encoded = StringUtil.encodeBase64(cipher.doFinal(source
                    .getBytes()));
        } catch (Exception e) {
            logger.log(Level.SEVERE, "CryptoUtil#encodeString exception: "
                    + e.getLocalizedMessage());
        }
        return encoded;
    }
}

Related Tutorials