2014-09-24 23 views
0

我需要使用下面的方法從KeyStore類:轉換的關鍵對象專用密鑰

public Key getPrivateKey(String alias)

但在最後,我需要專用密鑰對象,而不是重點。我如何將Key對象轉換爲PrivateKey? 謝謝!

+0

AFAIK它的安全投關鍵專用密鑰。 – 2014-09-24 19:11:17

回答

0

後,您從您的密鑰存儲 密鑰的密鑰創建密鑰對,然後得到一對密鑰的酒吧和私法重點如下:

Key prvkey = keyStore.getKey(_alias,_keypass.toCharArray()); 
PublicKey pubkey = certificate.getPublicKey(); 
KeyPair keypair = new KeyPair(pubkey, (PrivateKey)prvkey); 
PrivateKey privKewy = keypair.getPrivate(); 
0
//get keystore 
//jks for type "JKS", 
//.p12 or .pfx for type "PKCS12" 
//specification name is PKCS#12, but the # is not used in the Java keystore type name 
KeyStore keystore = KeyStore.getInstance("pkcs12"); 
//load keystore - is FileImputStream to location of your pfx/jks file   
keystore.load(is, password); 
//get private key   
PrivateKey privateKey = (PrivateKey)keystore.getKey(alias, password);