java.security.UnrecoverableKeyException:获取密钥失败:在Java中读取.p12文件时为null

时间:2013-11-14 11:07:56

标签: java keystore

我正在尝试从从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令牌的签名者。

提前致谢。

1 个答案:

答案 0 :(得分:1)

如果有人在这里登陆,如@prathamesh_mb所述,您需要在加载密钥时提供密码。

result_api