JAVA - How to store and read RSA public key from sqlite db

I need to save public key and private to sqlite database. Actually, I am writing pubKey.getEncoded () to the database, and to recreate 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 this gives me the following error:

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

in this moment:

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

Can anybody help me?

Thank you in advance

+3
source share
2 answers

: http://java.sun.com/docs/books/tutorial/jdbc/basics/retrieving.html, , . , char (n) .

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);
}

BLOB CLOB, . http://java.sun.com/j2se/1.5.0/docs/guide/jdbc/blob.html

+2
  • ResultSet, . next(), .

  • , .

0

Source: https://habr.com/ru/post/1752982/


All Articles