title: JAVA - How to store and read an RSA public key from an sqlite db
body: <p>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:</p>
  
  <blockquote>
  <pre><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);
  </code></pre>
  </blockquote>
  
  <p>but it gives me the following error</p>
  
  <blockquote>
    <p>Exception in thread "main" java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException: Detect premature EOF</p>
  </blockquote>
  
  <p>at this point :</p>
  
  <blockquote>
    <p>pubKey = (RSAPublicKey) rsaKeyFac.generatePublic(keySpec);</p>
  </blockquote>
  
  <p>Anyone can help me?</p>
  
  <p>Thank you in advance</p>