get Enhanced Secret Key - Android java.security

Android examples for java.security:Key

Description

get Enhanced Secret Key

Demo Code


//package com.java2s;

import java.security.NoSuchAlgorithmException;

import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;

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

public class Main {
    private static final String saltString = "** PUT YOUR OWN KEY PHRASE HERE !! **";

    public static SecretKey getEnhancedSecretKey(char[] passphraseOrPin)
            throws NoSuchAlgorithmException, InvalidKeySpecException {
        // Number of PBKDF2 hardening rounds to use. Larger values increase
        // computation time. You should select a value that causes computation
        // to take >100ms.
        final int iterations = 1000;

        final int outputKeyLength = 256;
        SecretKeyFactory secretKeyFactory = SecretKeyFactory
                .getInstance("PBKDF2WithHmacSHA1");
        KeySpec keySpec = new PBEKeySpec(passphraseOrPin,
                saltString.getBytes(), iterations, outputKeyLength);
        SecretKey secretKey = secretKeyFactory.generateSecret(keySpec);
        return secretKey;
    }//  w  w w  . j  av  a 2s  .c  o m
}

Related Tutorials