get Private Key from file - Java Security

Java examples for Security:Key

Description

get Private Key from file

Demo Code


//package com.java2s;

import java.io.FileInputStream;

import java.security.KeyStore;
import java.security.PrivateKey;

public class Main {


    public static final String KEY_STORE = "JKS";

    private static PrivateKey getPrivateKey(String keyStorePath,
            String alias, String password) throws Exception {
        KeyStore keyStore = getKeyStore(keyStorePath, password);
        PrivateKey privateKey = (PrivateKey) keyStore.getKey(alias,
                password.toCharArray());
        return privateKey;
    }//  w  w  w .j a va2 s  .  c  o m

    private static KeyStore getKeyStore(String keyStorePath, String password)
            throws Exception {
        FileInputStream in = new FileInputStream(keyStorePath);
        KeyStore keyStore = KeyStore.getInstance(KEY_STORE);
        keyStore.load(in, password.toCharArray());
        in.close();
        return keyStore;
    }
}

Related Tutorials