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
我拷贝了你的代码,编译不通过,因为找不到 OAuthSignatureMethod 这个类
也没有看到你 import