SargerasWang's Blog

我常常思考为什么鸟儿拥有整片天空,却常常停留在一个地方。然后我问了自己同样的问题。

generatePrivate Exception 解决

java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: invalid key format 异常解决

私钥是直接生成的pkcs8格式,类似如下:

1
2
3
MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAL21hi3tyVA2ILrk
...
+bojB4QWBQi9zg==

直接在代码中使用String存储,利用类似如下代码进行签名:

1
2
3
PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(prikeyvalue.getBytes());
KeyFactory keyf = KeyFactory.getInstance("RSA");
PrivateKey myprikey = keyf.generatePrivate(priPKCS8);

其中第三行报错,如下:

1
2
3
4
5
6
7
8
9
10
11
12
java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: invalid key format
  at sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:217)
  at java.security.KeyFactory.generatePrivate(KeyFactory.java:372)
  at ...
Caused by: java.security.InvalidKeyException: invalid key format
  at sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:330)
  at sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:356)
  at sun.security.rsa.RSAPrivateCrtKeyImpl.<init>(RSAPrivateCrtKeyImpl.java:91)
  at sun.security.rsa.RSAPrivateCrtKeyImpl.newKey(RSAPrivateCrtKeyImpl.java:75)
  at sun.security.rsa.RSAKeyFactory.generatePrivate(RSAKeyFactory.java:316)
  at sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:213)
  ... 77 more

解决方式,秘钥String要先经过Base64 Decode,如下:

1
2
3
PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(prikeyvalue.getBytes());
//改成如下
PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(new Base64().decode(prikeyvalue.getBytes()));

这里的Base64类是指org.apache.commons.codec.binary.Base64.

问题解决