【发布时间】: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”。然后它工作正常。