7

我正在尝试从从 Google 服务帐户控制台下载的 .p12 文件中提取 privateKey。

我正在从文件夹位置读取这个 .p12 并创建 KeyStore 对象。

KeyStore myStore = null;
FileInputStream in_cert = new FileInputStream(
                "D://Google Analytics//login//GA//6aa55af54232dfa60b60a908ff0138bba1147d63-privatekey.p12");


        myStore = KeyStore.getInstance("PKCS12");
        myStore.load(in_cert, null); // if i give "changeit" as the password(hopefully the default keystore password) gets the exception "java.security.UnrecoverableKeyException: Get Key failed: Given final block not properly padded. If null is passed as an argument the program runs fine with output as "privatekey""

        Enumeration<String> aliases = myStore.aliases();
        String alias = "";
        System.out.println(myStore.aliases());

        while (aliases.hasMoreElements()) {
            System.out.println(aliases.nextElement()); // prints privatekey
        }

        java.security.PrivateKey privateKey = (PrivateKey) myStore.getKey("privatekey", null); // gives exception "java.security.UnrecoverableKeyException: Get Key failed: null"

我在做什么错了?

我无处可去,因为此私钥将用作创建 Google JSON Web 令牌的签名者。

提前致谢。

4

1 回答 1

6

万一有人登陆这里,正如@prathamesh_mb所提到的,您需要在加载密钥时提供密码。

PrivateKey privateKey = (PrivateKey) myStore.getKey("privatekey", "yourpasswordhere".toCharArray());
于 2018-03-23T10:03:04.997 回答