Getting started with the Java operations KeyStore
1. Using Java Keytool This key and certificate management tool, create a key and deposit it into the. keystore file, for example, there will be an entry with an alias of "MyKey" by default, or you can create it yourself.
2. Create a KeyStore instance, encrypt the KeyStore from the. keystore file, and then obtain the corresponding entry through the alias, making sure that you have the correct password.
public class Keystoretest {public static void main (string[] args) throws exception{//load KeyStore file FileInputStream in = new file InputStream ("C:\\data\\.keystore"); System.out.println (Keystore.getdefaulttype ()); KeyStore ks = Keystore.getinstance (Keystore.getdefaulttype ());//load KeyStore with correct password//otherwise java.io.IOException:Keystore was tampered with, or password is incorrectks.load (in, "Vonzhou". ToCharArray ());//Get the alias "MyKey" corresponding to the private key Privatekey key = ( Privatekey) Ks.getkey ("MyKey", "Vonzhou". ToCharArray ());//or so protectionparameter protection = new Keystore.passwordprotection ("Vonzhou". ToCharArray ()); Keystore.privatekeyentry pkentry = (keystore.privatekeyentry) ks.getentry ("MyKey", protection); Privatekey Key2 = Pkentry.getprivatekey (); System.out.println (Arrays.tostring (key.getencoded ())); System.out.println (Arrays.tostring (key2.getencoded ())); In.close ();}}
Reference:
1.keytool
Getting started with the Java operations KeyStore