Build secret key. - Java Security

Java examples for Security:Key

Description

Build secret key.

Demo Code


//package com.java2s;

import java.security.GeneralSecurityException;

import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;

public class Main {

    /**/* w w  w .  j  a va2  s . c o  m*/
     * Cipher algorithm to use. "AES"
     */
    private static final String CIPHER_ALGORITHM = "AES";

    /**
     * Build secret key.
     *
     * @param clientId
     *            client ID (used for creating the {@link SecretKey})
     * @param clientSecret
     *            client secret (used for creating the {@link SecretKey})
     * @return a secret key
     * @throws GeneralSecurityException
     *             crypto API problem
     */
    public static SecretKey buildSecretKey(final String clientId,
            final String clientSecret) throws GeneralSecurityException {

        final PBEKeySpec pbeSpec = new PBEKeySpec(
                clientSecret.toCharArray(), clientId.getBytes(), 42, 128);

        final SecretKeyFactory factory = SecretKeyFactory
                .getInstance("PBKDF2WithHmacSHA1");
        return new SecretKeySpec(factory.generateSecret(pbeSpec)
                .getEncoded(), CIPHER_ALGORITHM);
    }
}

Related Tutorials