tags:

views:

65

answers:

2

Hi, i have to store a public key and a private one into a sqlite database. Actually i write the pubKey.getEncoded() into the database, and to recreate the pubkey i use the following code:

    ResultSet result = stat.executeQuery("select publickey from cert where id='1'");
    KeyFactory rsaKeyFac =  KeyFactory.getInstance("RSA");
    X509EncodedKeySpec keySpec = new X509EncodedKeySpec(result.getBytes(1));
    RSAPublicKey pubKey;
    pubKey = (RSAPublicKey) rsaKeyFac.generatePublic(keySpec);

but it gives me the following error

Exception in thread "main" java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException: Detect premature EOF

at this point :

pubKey = (RSAPublicKey) rsaKeyFac.generatePublic(keySpec);

Anyone can help me?

Thank you in advance

A: 
  1. When you first get a ResultSet, the cursor points to the space before the first row of results. So you need to call next() on it before trying to retrieve the data.

  2. You should make sure that the query actually succeeded.

Mike Baranczak
you're right, but nothing has changed, the error is still presentthe query correctly works
michele
+1  A: 

If you look here: http://java.sun.com/docs/books/tutorial/jdbc/basics/retrieving.html you will see how to properly retrieve from a DB. That is assuming you have a type of char(n) for your DB column.

ResultSet result = stat.executeQuery("select publickey from cert where id='1'");
while(result.next())
{
   KeyFactory rsaKeyFac =  KeyFactory.getInstance("RSA");
   X509EncodedKeySpec keySpec = new X509EncodedKeySpec(result.getString(1));
   RSAPublicKey pubKey;
   pubKey = (RSAPublicKey) rsaKeyFac.generatePublic(keySpec);
}

If the type is a BLOB or CLOB then you need to use a different mechanism. http://java.sun.com/j2se/1.5.0/docs/guide/jdbc/blob.html

Romain Hippeau
It should be a BLOB. The getEncoded() encoding that is used produces bytes than can and will span the entire range from 0x00 to 0xff, and cannot be stored as character or string data.
GregS
Romain:Your code is very similar to mine, and the result is similar too, it doesn't works. Same Exception.
michele
GregS :I've tried with the blob type, in the following way:>result.next(); KeyFactory rsaKeyFac=null; rsaKeyFac = KeyFactory.getInstance("RSA"); Blob pukey=result.getBlob(1); int leng=(int)pukey.length(); X509EncodedKeySpec keySpec = new X509EncodedKeySpec(pukey.getBytes(1,leng)); RSAPublicKey pubKey; pubKey = (RSAPublicKey) rsaKeyFac.generatePublic(keySpec);i've got a new exception which say : java.sql.SQLException: not implemented by SQLite JDBC driverso i think the blob exception is a sqlite limitation.But the problem still persist, i'm not able to retrieve the pubkey!
michele
@michele: If the pubkey was stored as a character object, then it may have been corrupted by the process. Character sets are limited but the public key bytes can take on any value. The public key *should* have been stored as a binary type, probably a BLOB.
GregS