【发布时间】:2016-10-11 03:08:34
【问题描述】:
私钥生成
public PrivateKey getStoredPrivateKey(String filePath) {
PrivateKey privateKey = null;
byte[] keydata = getKeyData(filePath);
PKCS8EncodedKeySpec encodedPrivateKey = new PKCS8EncodedKeySpec(keydata);
KeyFactory keyFactory = null;
try {
keyFactory = KeyFactory.getInstance("RSA");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
try {
System.out.println("hello");
privateKey = keyFactory.generatePrivate(encodedPrivateKey);
} catch (InvalidKeySpecException e) {
e.printStackTrace();
}
return privateKey;
}
我在这里使用它
PrivateKey privateKey = new KryptoUtil().getStoredPrivateKey(privateKeyFilePath);
但显示错误
hello
java.security.spec.InvalidKeySpecException:
java.security.InvalidKeyException: IOException : version mismatch: (supported: 00, parsed: 03
at sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(Unknown Source)
我在 getStoredPrivateKey(String filePath) 函数中传递一个 (.p12) 文件。
为什么会报错?
【问题讨论】:
-
您确定您的私钥符合规范 PKCS#8。尝试使用其他规范,如 RSAPrivateKeySpec
-
我怎样才能确定它不是 PKCS#8
标签: java key private-key key-pair