create AES Secret Key - Android java.security

Android examples for java.security:AES

Description

create AES Secret Key

Demo Code


//package com.java2s;
import java.io.UnsupportedEncodingException;

import javax.crypto.spec.SecretKeySpec;

public class Main {

    private static SecretKeySpec createKey(String password) {
        byte[] data = null;
        if (password == null) {
            password = "";
        }/*w  w w.  j a v  a 2 s.  co m*/
        StringBuffer sb = new StringBuffer(32);
        sb.append(password);
        while (sb.length() < 32) {
            sb.append("0");
        }
        if (sb.length() > 32) {
            sb.setLength(32);
        }

        try {
            data = sb.toString().getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return new SecretKeySpec(data, "AES");
    }
}

Related Tutorials