這是我的程序。我收到錯誤「打開密鑰文件時出錯」。請幫助我在哪裏出錯。如何使用java sun security從.p12文件中提取私鑰
import java.io.FileInputStream;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.PrivateKey;
import java.util.Base64;
public class ExtractionClass {
public static void main(String args[]) {
String p12Password = "PassWord";
try{
FileInputStream file = new FileInputStream("C:/Users/Aarti_Jangid/Desktop/p12/certChain.p12");
System.out.println("FILE "+ file);
KeyStore keystore = KeyStore.getInstance("PKCS12", "SunJSSE");
keystore.load(file, p12Password.toCharArray());
String alias = keystore.aliases().nextElement();
PrivateKey key = (PrivateKey)keystore.getKey(alias, p12Password.toCharArray());
System.out.println(Base64.getEncoder().encodeToString(key.getEncoded()));
} catch(KeyStoreException kse) {
kse.printStackTrace();
System.err.println("Error getting the key");
} catch (Exception e) {
System.err.println("Error opening the key file");
e.printStackTrace();
}
}
}
請複製以及 –
我覺得你的.p12文件有什麼不對粘貼在這裏堆棧跟蹤,如果您使用有效的證書,則您的代碼可以在[此處](https://itv.mit-xperts.com/clientssl/issue/dload/index.php?id=1411134668)上找到有效的證書。 – user6904265
如果以編程方式創建.p12文件,請查看[這裏](http://stackoverflow.com/questions/5312559/how-do-i-programmatically-create-a-new-keystore) – user6904265