Java操作证书


import java.io.FileInputStream;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.cert.Certificate;
import java.util.Enumeration;


public
static void GetPrivateKey() { try{ KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType()); keystore.load(new FileInputStream("C:\\Program Files\\Java\\jre7\\bin\\xiaoyaodijun.keystore"), "7391428".toCharArray()); KeyPair keyPair = getPrivateKey2(keystore, "xiaoyaodijun.keystore", "7391428".toCharArray()); PrivateKey privateKey = keyPair.getPrivate(); System.out.println("private key = " + privateKey); }catch(Exception ex){} } public static KeyPair getPrivateKey2(KeyStore keystore, String alias, char[] password) { try { Key key=keystore.getKey(alias,password); if(key instanceof PrivateKey) { Certificate cert=keystore.getCertificate(alias); PublicKey publicKey=cert.getPublicKey(); return new KeyPair(publicKey,(PrivateKey)key); } }catch (Exception e) { } return null; }

 

posted @ 2019-01-20 22:34  太素元清  阅读(148)  评论(0编辑  收藏  举报