generate AES Key Spec - Android java.security

Android examples for java.security:AES

Description

generate AES Key Spec

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 byte[] KEY_BYTES = { 0x00, 0x01, 0x02, 0x03, 0x04,
            0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
            0x0f };/*from ww  w  .  j  a  v  a 2 s.c om*/
    private final static String SECRET_KEY = "secret.key";
    private static Logger logger = Logger.getLogger(CryptoUtil.class
            .getPackage().getName());
    private static SecretKeySpec generateKeySpec(final Context context) {
        SecretKeySpec keySpec = null;
        try {
            keySpec = new SecretKeySpec(KEY_BYTES, 0, 16, "AES");
            FileOutputStream fileStream = context.openFileOutput(
                    SECRET_KEY, Context.MODE_PRIVATE);
            new FileOutputStream("secret.key", false);
            ObjectOutputStream objectStrem = new ObjectOutputStream(
                    fileStream);
            objectStrem.writeObject(keySpec);
            objectStrem.close();
            fileStream.close();
        } catch (Exception e) {
            logger.log(Level.SEVERE, "CryptoUtil#createKeyPair exception: "
                    + e.getLocalizedMessage());
        }
        return keySpec;
    }
}

Related Tutorials