AES Secret key generate - Android java.security

Android examples for java.security:AES

Description

AES Secret key generate

Demo Code


//package com.java2s;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import java.util.Arrays;

import javax.crypto.spec.SecretKeySpec;

public class Main {
    public static SecretKeySpec keygenerate(String passphrase) {
        SecretKeySpec sks = null;
        try {/*from ww  w. ja  v a 2  s .c  o m*/
            byte[] key = passphrase.getBytes("UTF-8");
            MessageDigest sha = MessageDigest.getInstance("SHA-1");
            key = sha.digest(key);
            key = Arrays.copyOf(key, 16); // use only first 128 bit

            sks = new SecretKeySpec(key, "AES");
        } catch (NoSuchAlgorithmException e) {
        } catch (UnsupportedEncodingException e) {
        }
        return sks;
    }
}

Related Tutorials