【问题标题】:java.security.UnrecoverableKeyException: Get Key failed: null while reading .p12 file in Javajava.security.UnrecoverableKeyException:获取密钥失败:在 Java 中读取 .p12 文件时为空
【发布时间】:2013-11-27 08:53:56
【问题描述】:

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

提前致谢。

【问题讨论】:

  • 对不起各位。得到了答案。应为 getKey(alias, password) 提供密码“notasecret”。然后它工作正常。

标签: java keystore


【解决方案1】:

以防万一有人登陆,正如@prathamesh_mb 所述,您需要在加载密钥时提供密码。

PrivateKey privateKey = (PrivateKey) myStore.getKey("privatekey", "yourpasswordhere".toCharArray());

【讨论】:

    猜你喜欢
    • 2016-08-07
    • 1970-01-01
    • 2023-04-05
    • 2016-09-28
    • 2021-09-24
    • 1970-01-01
    • 1970-01-01
    • 2021-11-16
    • 1970-01-01
    相关资源
    最近更新 更多