package test;import java.net.URL;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.spec.EncodedKeySpec;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Map;
import java.util.Collection;public class GoogleOAuthExample {
private static final String PRIVATE_KEY = "c9a935155644906cd31f21fe54a709be04b56ac6a"; public static void main(String[] args) throws Exception {
KeyFactory fac = KeyFactory.getInstance("RSA");
 EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(OAuthSignatureMethod.decodeBase64(PRIVATE_KEY));
fac = KeyFactory.getInstance("RSA");
PrivateKey privateKey = fac.generatePrivate(privKeySpec);
}
}//报错如下Exception in thread "main" java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException : DerInputStream.getLength(): lengthTag=86, too big.
at sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(Unknown Source)
at java.security.KeyFactory.generatePrivate(Unknown Source)
at test.GoogleOAuthExample.main(GoogleOAuthExample.java:41)
Caused by: java.security.InvalidKeyException: IOException : DerInputStream.getLength(): lengthTag=86, too big.
at sun.security.pkcs.PKCS8Key.decode(Unknown Source)
at sun.security.pkcs.PKCS8Key.decode(Unknown Source)
at sun.security.rsa.RSAPrivateCrtKeyImpl.<init>(Unknown Source)
at sun.security.rsa.RSAPrivateCrtKeyImpl.newKey(Unknown Source)
at sun.security.rsa.RSAKeyFactory.generatePrivate(Unknown Source)
... 3 more

解决方案 »

  1.   

    为什么没有看到你有import 这个类啊?--》 OAuthSignatureMethod
    我拷贝了你的代码,编译不通过,因为找不到 OAuthSignatureMethod 这个类
    也没有看到你 import 
      

  2.   

    代码拷贝少了,import net.oauth.signature.OAuthSignatureMethod;这个ApacheJMeter_oauth-v2.jar
      

  3.   

    像 RSA 1024 位的私钥有 5000 多个 bit,RSA 2048 位的私钥有 9000 多个 bit
      

  4.   

    诚邀 编程高手 加入 问问团队——编程狂,地址:http://wenwen.soso.com/t/t260701.htm