How can I store and read a public key from the database? I use postgresql and java

advertisements

Hi I need to store and read public keys from a postgresql data base and i am using java.

I have this code to store

 cbd.ejecutarSeguro("update persona set llave_publica = ? where cedula = 112345678", x509EncodedKeySpec.getEncoded());

    public void ejecutarSeguro(String query, byte[] pkBytes) {
        try {
            Connection conn = DriverManager.getConnection(nombreConexion);
            PreparedStatement pstat;

            pstat = conn.prepareStatement(query);
            pstat.setBytes(1, pkBytes);
            pstat.execute();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

To read i use this

String filePublicKey_s = cbd.consultarArray("Select llave_publica from persona where cedula = 112345678").get(0).get(0);
        byte[] encodedPublicKey = new byte[(int) filePublicKey.length()];
        //encodedPublicKey = (filePublicKey_s).substring(2).getBytes();
        System.out.println("Public Key; "+filePublicKey_s);
        System.out.println("rescato de la base; "+(filePublicKey_s).getBytes());// LA que rescato de la base
        fis.read(encodedPublicKey);
        System.out.println("Public Key; "+encodedPublicKey);
        fis.close();

In example I have the public key in a file in my computer... when I compare the output I see this

Public Key: 308201b83082012c06072a8648ce3804013082011f02818100fd7f53811d75122952df4a9c2eece4e7f611b7523cef4400c31e3f80b6512669455d402251fb593d8d58fabfc5f5ba30f6cb9b556cd7813b801d346ff26660b76b9950a5a49f9fe8047b1022c24fbba9d7feb7c61bf83b57e7c6a8a6150f04fb83f6d3c51ec3023554135a169132f675f3ae2b61d72aeff22203199dd14801c70215009760508f15230bccb292b982a2eb840bf0581cf502818100f7e1a085d69b3ddecbbcab5c36b857b97994afbbfa3aea82f9574c0b3d0782675159578ebad4594fe67107108180b449167123e84c281613b7cf09328cc8a6e13c167a8b547c8d28e0a3ae1e2bb3a675916ea37f0bfa213562f1fb627a01243bcca4f1bea8519089a883dfe15ae59f06928b665e807b552564014c3bfecf492a038185000281810096e492622ca7f93f51b96d44f15e69f6711b1eecc308f4b017a0e12b46a722b1b662a072f0cdf61b6853595f66eed2e697a4ba2af60f4eb78a24a169513acc6238dcbf6dc898a271d7f91b4b6ea4d2a5c504ee6970c7cd565d414d14305c786385558f06e47f3d2744fa0d73acdc3ca75e435a3a70dcebb1f997a02e7e96ab30
Public Key; \x308201b83082012c06072a8648ce3804013082011f02818100fd7f53811d75122952df4a9c2eece4e7f611b7523cef4400c31e3f80b6512669455d402251fb593d8d58fabfc5f5ba30f6cb9b556cd7813b801d346ff26660b76b9950a5a49f9fe8047b1022c24fbba9d7feb7c61bf83b57e7c6a8a6150f04fb83f6d3c51ec3023554135a169132f675f3ae2b61d72aeff22203199dd14801c70215009760508f15230bccb292b982a2eb840bf0581cf502818100f7e1a085d69b3ddecbbcab5c36b857b97994afbbfa3aea82f9574c0b3d0782675159578ebad4594fe67107108180b449167123e84c281613b7cf09328cc8a6e13c167a8b547c8d28e0a3ae1e2bb3a675916ea37f0bfa213562f1fb627a01243bcca4f1bea8519089a883dfe15ae59f06928b665e807b552564014c3bfecf492a038185000281810096e492622ca7f93f51b96d44f15e69f6711b1eecc308f4b017a0e12b46a722b1b662a072f0cdf61b6853595f66eed2e697a4ba2af60f4eb78a24a169513acc6238dcbf6dc898a271d7f91b4b6ea4d2a5c504ee6970c7cd565d414d14305c786385558f06e47f3d2744fa0d73acdc3ca75e435a3a70dcebb1f997a02e7e96ab30
From data base; [[email protected]
From file; [[email protected]

If I read the PK from the database i read an \x and all the PK change

thanks


Convert the PublicKey into byte array

    byte[] bytes = publicKey.getEncoded();

write the bytes to a BLOB or BINARY column

...
PreparedStatement ps = conn.prepareStatement("update tbl1 set col1=?");
ps.setBytes(1, bytes);
ps.executeUpdate();

read it back to a byte array

...
byte[] bytes = resultSet.getBytes(1);

convert bytes back to PublicKey

KeyFactory keyFactory = KeyFactory.getInstance(alg);
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(bytes);
PublicKey pk = keyFactory.generatePublic(publicKeySpec);