Here you can find the source of generateKey(final String password, final byte salt[])
Parameter | Description |
---|---|
password | a parameter |
salt | a parameter |
Parameter | Description |
---|---|
Exception | an exception |
public static SecretKey generateKey(final String password, final byte salt[]) throws Exception
//package com.java2s; //License from project: Open Source License import java.security.spec.KeySpec; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.PBEKeySpec; import javax.crypto.spec.SecretKeySpec; public class Main { public static final int KEY_SIZE = 128; public static final int SALT_LENGTH = 8; /**/*from w ww . j a va 2 s. c o m*/ * Generates a key using the specified password and salt. * @param password * @param salt * @return * @throws Exception */ public static SecretKey generateKey(final String password, final byte salt[]) throws Exception { if (salt.length < SALT_LENGTH) { throw new Exception("Need a salt at least length " + SALT_LENGTH + "."); } return generateKey(password.toCharArray(), salt); } /** * Generates a key using the specified password and salt. * @param password * @param salt * @return * @throws Exception */ public static SecretKey generateKey(final char password[], final byte salt[]) throws Exception { if (salt.length != 8) { throw new Exception("Invalid salt length."); } SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); KeySpec spec = new PBEKeySpec(password, salt, 65536, KEY_SIZE); SecretKey tmp = factory.generateSecret(spec); return new SecretKeySpec(tmp.getEncoded(), "AES"); } }