ITeye博客

狗日的支付宝

wenjinglian · 2013-04-24 23:26 · JAVA · 编程语言 · RSA · Markdown

用openssl 生成的密钥老是报以下错误:

java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException : DER input, Integer tag error
	at sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:200)
	at java.security.KeyFactory.generatePrivate(KeyFactory.java:342)
	at com.qhx.component.payment.payplatform.alipay.util.RSASignature.getPrivateKey(RSASignature.java:84)
	at com.qhx.component.payment.payplatform.alipay.util.RSASignature.main(RSASignature.java:161)
Caused by: java.security.InvalidKeyException: IOException : DER input, Integer tag error
	at sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:344)
	at sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:350)
	at sun.security.rsa.RSAPrivateCrtKeyImpl.<init>(RSAPrivateCrtKeyImpl.java:74)
	at sun.security.rsa.RSAPrivateCrtKeyImpl.newKey(RSAPrivateCrtKeyImpl.java:58)
	at sun.security.rsa.RSAKeyFactory.generatePrivate(RSAKeyFactory.java:299)
	at sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:196)
	... 3 more

后来发现,原来是支付宝,生成的命令有问题,参数放错位置,害死人。

 


 

 

 将(3) 的命令改为:

openssl pkcs8 -topk8 -nocrypt -inform PEM -in rsa_private_key.pem -outform PEM outform

 

完整命令:

1.

openssl genrsa -out rsa_private_key.pem 1024

2.

openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem 

 

3.openssl pkcs8 -topk8 -nocrypt -inform PEM -in rsa_private_key.pem -outform PEM outform

评论(5)

  1. #5 楼 · littlesheep · 2015-12-28 11:33

    我按照你的方法从新生成了密钥可是还是报者个错误...

  2. #4 楼 · fushcpc · 2015-05-06 16:49

    在Mac上测试与参数的顺序无关, 加入下面的代码可以解决

    
    java.security.Security.addProvider(
    			         new org.bouncycastle.jce.provider.BouncyCastleProvider()
    			);
    
    
  3. #3 楼 · wenjinglian · 2014-09-12 17:29

    helmy000 writes:

    狗日的支付宝,我就这个问题卡了几个小时

    其实这个问题还与系统有关,有些xp可以有些不可以,不过出现这问题,按我这个方式可以成功

  4. #2 楼 · helmy000 · 2014-09-12 16:54

    狗日的支付宝,我就这个问题卡了几个小时

  5. #1 楼 · Jeffie · 2014-01-23 20:57

    狗日的支付宝太坑爹了!

评论功能维护中,当前仅展示历史评论。