generate AES Key from password - Android java.security

Android examples for java.security:AES

Description

generate AES Key from password

Demo Code


//package com.java2s;

import javax.crypto.spec.SecretKeySpec;

public class Main {

    private static SecretKeySpec generateAESKey(String password) {
        byte[] data = null;
        StringBuilder sb = new StringBuilder();
        sb.append(password);/*from   ww w.j  a v  a 2  s  . c om*/
        while (sb.length() < 16)
            sb.append("0");
        if (sb.length() > 16)
            sb.setLength(16);
        try {
            data = sb.toString().getBytes("UTF-8");
            return new SecretKeySpec(data, "AES");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

Related Tutorials