2014-09-02 412 views
1

背景无效密钥格式:java.security.InvalidKeyException:上生成RSA公钥

我已经创建了一个小程序来提取从智能卡中提取的证书的公钥。 这个公钥然后存储在数据库中。 证书的私钥用于签署数据,然后使用公钥验证签名。用于从证书中提取公开密钥 代码:

private byte[] getPublicKey(KeyStore paramKeyStore) 
    throws GeneralSecurityException { 
    Enumeration localEnumeration = paramKeyStore.aliases(); 

    if (localEnumeration.hasMoreElements()) { 
    String element = (String) localEnumeration.nextElement(); 
    Certificate[] arrayOfCertificate = 
     paramKeyStore.getCertificateChain(element); 
    byte[] publicKeyByteArray = 
     arrayOfCertificate[0].getPublicKey().getEncoded(); 

    return publicKeyByteArray; 
    } 
    throw new KeyStoreException("The keystore is empty!"); 
} 

这publicKeyByteArray然后在数据库中作为BLOB storeed使用bytes2String方法转换为字符串后:

private static String bytes2String(byte[] bytes) { 
    StringBuilder string = new StringBuilder(); 
    for (byte b : bytes) { 
    String hexString = Integer.toHexString(0x00FF & b); 
    string.append(hexString.length() == 1 ? "0" + hexString : hexString); 
    } 
    return string.toString(); 
} 

的BLOB的含量(密钥)保存在数据库是:

30820122300d06092a864886f70d01010105000382010f003082010a02820101009bd307e4fc38adae43b93ba1152a4d6dbf82689336bb4e3af5160d16bf1599fe070f7acbfefd93e866e52043de1620bd57d9a3f244fb4e6ef758d70d19e0be86e1b12595af748fbc00aad9009bd61120d3348079b00af8462de46e254f6d2b092cbc85c7f6194c6c37f8955ef7b9b8937a7e9999541dbbea8c1b2349c712565482dbd573cd9b7ec56a59e7683b4c246620cf0d8148ed38da937f1e4e930eb05d5b4c6054712928fa59870763468c07e71265525e1e40839b51c833579f5742d3c8e0588766e3ed6deef1593b10baad0a2abea34734de1505d37710e1cfaa4225b562b96a6a4e87fecb1d627d4c61916e543eba87054ee9212e8183125cdb49750203010001 

读取所存储的公共密钥字节后[]从数据库中,我尝试使用弗洛将其转换回公钥翼码:

Cipher rsa; 
rsa = Cipher.getInstance("RSA"); 
KeyFactory keyFactory = KeyFactory.getInstance("RSA"); 
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(pkey.getBytes()); 
PublicKey pk = keyFactory.generatePublic(publicKeySpec); 
rsa.init(Cipher.DECRYPT_MODE, pk); 
byte[] cipherDecrypt = rsa.doFinal(encryptedText.getBytes()); 

但它提供了以下错误:

Caused by: java.security.InvalidKeyException: invalid key format 
    at sun.security.x509.X509Key.decode(X509Key.java:387) 
    at sun.security.x509.X509Key.decode(X509Key.java:403) 
    at sun.security.rsa.RSAPublicKeyImpl.<init>(RSAPublicKeyImpl.java:83) 
    at sun.security.rsa.RSAKeyFactory.generatePublic(RSAKeyFactory.java:298) 
    at sun.security.rsa.RSAKeyFactory.engineGeneratePublic(RSAKeyFactory.java:201) 

请提出这个问题的原因和解决方案。

+3

“*用户提供的加密文本**使用存储的数据库公钥**解密,得到的解密文本与认证目的相匹配。”公钥不能解密,只能加密。用户*用私钥对*数据进行签名,在这种情况下,您可以使用公钥来验证签名。或者用户使用公钥加密数据,并且需要私钥解密。我猜这是前一种情况? – 2014-09-02 12:04:41

+0

嗨,是的,这是你提到的第一例。私钥用于签署数据,然后使用公钥来验证签名。我相应地更新了我的问题。 – user3619997 2014-09-02 12:17:21

+0

你做错了。证书应该随签名一起提供,实际上签名应该在其签署的数据中包含证书。然后可以直接从证书中获取公钥:您无需将其存储在数据库中。 – EJP 2014-09-02 12:28:45

回答

3

从数据库中读回密钥的方式必须有错误。下面的代码工作对我蛮好:

String key = "3082012230..."; // full key omitted for brevity 
byte[] derPublicKey = DatatypeConverter.parseHexBinary(key); 

KeyFactory keyFactory = KeyFactory.getInstance("RSA"); 
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(derPublicKey); 
keyFactory.generatePublic(publicKeySpec); 

我猜,基于使用的pkey.getBytes(),那你只是试图让字符串中的字节,而不是十六进制对其进行解码。

+0

谢谢!它正在工作。 – user3619997 2014-09-02 12:34:11

+0

私钥怎么样? @ user3619997 – 2017-10-31 10:04:33