Getting a PrivateKey object from a .p12 file in Java

前端 未结 4 1634
[愿得一人]
[愿得一人] 2020-12-05 06:59

As the title suggests, I have .p12 file required for google service account api access. In order to get the credential to connect to the api, there\'s a field .setServiceAcc

4条回答
  •  一向
    一向 (楼主)
    2020-12-05 07:36

    If you get null from getKey() (eg. you are using BouncyCastle as a provider) you should find the last keyAlias element:

    KeyStore keystore = KeyStore.getInstance("PKCS12", "BC");
    keystore.load(this.getClass().getClassLoader().getResourceAsStream("keyFile.p12"), p12Password.toCharArray());
    Enumeration aliases = keystore.aliases();
    String keyAlias = "";
    while (aliases.hasMoreElements()) {
        keyAlias = (String) aliases.nextElement();
    }
    PrivateKey key = (PrivateKey)keystore.getKey(keyAlias, pass);
    

提交回复
热议问题