从作为字符串存储的RSA公钥初始化PublicKey时出现异常(适用于笔记本电脑,不适用于服务器)

时间:2013-04-25 03:52:21

标签: java rsa public-key

我正在处理一个处理证书问题的应用程序,当我在笔记本电脑上本地运行JUnit测试但我实际将代码部署到服务器时,我遇到的问题是我的代码工作正常我得到一个例外。从存储在String中的RSA公钥创建PublicKey时发生异常。我希望有人能够指出我的方向,看看为什么它在服务器上失败但在Junit中工作。

我的RSA公钥:

-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvNEz3+TStAAndHTc1qwT
NGvZYyB7DD1FshQf+mbQUGJ9HccOXNn5oHB7fWQjODjlDrYyCs7FclSMTLxA3lHX
98QWeWHL2O8t8qrJQQEUWZITmr/ddiNJOOvMeYF0K5if4m84vjgx/pTwwAVyU0Yo
MMXPnRozO8o7zSyRsH4jixALDugrsveEjLQI/cIEFvNjqlhyfumHyJKywNkMH1oJ
4e/f89FkpeDV694lsLs1jguuLLnvroXYJ5Uzeos+F0Pj1zFDUvhWrjVwxsUfAxS8
5uFGTUm6EEl9XiKwi+mgg8ODrY5dh3uE2yKB2T1Qj8BfK55zB8cYbORSsm6/f6Bi
BwIDAQAB
-----END PUBLIC KEY-----

我的Junit代码:< - 从笔记本电脑上工作

@Test
public void testkey() throws Exception {
    String pem = "-----BEGIN PUBLIC KEY-----"+
                "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvNEz3+TStAAndHTc1qwT"+
                "NGvZYyB7DD1FshQf+mbQUGJ9HccOXNn5oHB7fWQjODjlDrYyCs7FclSMTLxA3lHX"+
                "98QWeWHL2O8t8qrJQQEUWZITmr/ddiNJOOvMeYF0K5if4m84vjgx/pTwwAVyU0Yo"+
        "MMXPnRozO8o7zSyRsH4jixALDugrsveEjLQI/cIEFvNjqlhyfumHyJKywNkMH1oJ"+
        "4e/f89FkpeDV694lsLs1jguuLLnvroXYJ5Uzeos+F0Pj1zFDUvhWrjVwxsUfAxS8"+
        "5uFGTUm6EEl9XiKwi+mgg8ODrY5dh3uE2yKB2T1Qj8BfK55zB8cYbORSsm6/f6Bi"+
        "BwIDAQAB"+
        "-----END PUBLIC KEY-----";
    PublicKey willNotWork = decodeKey(pem); 
}

我的实际代码:< - 在服务器上运行时抛出异常

@Override
public PublicKey getKey() {
    PublicKey pk = null;
     try {
        String crt =  "-----BEGIN PUBLIC KEY-----"+
                "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvNEz3+TStAAndHTc1qwT"+
                "NGvZYyB7DD1FshQf+mbQUGJ9HccOXNn5oHB7fWQjODjlDrYyCs7FclSMTLxA3lHX"+
                "98QWeWHL2O8t8qrJQQEUWZITmr/ddiNJOOvMeYF0K5if4m84vjgx/pTwwAVyU0Yo"+
                "MMXPnRozO8o7zSyRsH4jixALDugrsveEjLQI/cIEFvNjqlhyfumHyJKywNkMH1oJ"+
        "4e/f89FkpeDV694lsLs1jguuLLnvroXYJ5Uzeos+F0Pj1zFDUvhWrjVwxsUfAxS8"+
        "5uFGTUm6EEl9XiKwi+mgg8ODrY5dh3uE2yKB2T1Qj8BfK55zB8cYbORSsm6/f6Bi"+
        "BwIDAQAB"+
        "-----END PUBLIC KEY-----";
        System.out.append("crt == " + crt);
        pk = decodeKey(crt);
    }  catch(Exception ex){
        System.out.println("getKey caught: " + ex.getMessage()); 
    }  
    return pk;
}

decodeKey代码:

public PublicKey decodeKey(String text) throws CryptographyException {
    String content = text;
    if( text.contains("-----BEGIN PUBLIC KEY-----") ) {
      content = text.replace("-----BEGIN PUBLIC KEY-----", "").replace("-----END PUBLIC KEY-----", "");
    }
    if( text.contains("-----BEGIN RSA PUBLIC KEY-----") ) {
      content = text.replace("-----BEGIN RSA PUBLIC KEY-----", "").replace("-----END RSA PUBLIC KEY-----", "");
    }
    byte[] der = Base64.decodeBase64(content);
    return decodeDerKey(der);
}

public PublicKey decodeDerKey(byte[] publicKeyBytes) throws CryptographyException {
   try {
      KeyFactory factory = KeyFactory.getInstance("RSA"); 
      PublicKey publicKey  = factory.generatePublic(
                                 new X509EncodedKeySpec(publicKeyBytes)); 
      return publicKey;
    }
    catch(Exception e) {
        throw new CryptographyException(e);
    }
}

这是我在服务器上运行代码时抛出的异常:

Could not parse certificate: java.io.IOException: DerInputStream.getLength():
lengthTag=111, too big`

所以我猜我的笔记本电脑和服务器之间必须有某种区别。我已经验证两者都运行基本相同的Java版本(“1.7.0_17”)。我还检查了KeyFactory的提供者,它们也是一样的。那我从哪里看?

0 个答案:

没有答案