What method to store and read x509 PEM from db?
Hi,
What is the best method to read an X509 credential in .PEM format, and store it in a MySQL database using Java? After encountering problems with treating the credentail as a String, am now passing it in {and reading it out} as a byte array. The resulting credential appears to be truncated at the first line when I read it back:
Cert is: [B@174cc1f
I am storing the data in a MySQL varchar field of 2055 characters.
Do I have to Base64 encode and decode the Cert? I have not done that since
I just want to store the cert in the DB, not use it there; and base64 encoded data is ASCII characters.
My two method below, one is to load the pem public key to the db and the second is to read it from the db:
//Method to load the credential into db
public void uploadCredential(String userNameIn, byte[] credIn) {
dbi.singleInsert("INSERT INTO testcert (username, usercert) VALUES ("
+ "'" + userNameIn + "'" + "," + "'" + credIn + "'" + ")");
}
//Method to read the credential from the database
public void readUploadedCredentials(String userNameIn) {
System.out.println("SELECT * FROM testcert WHERE username =" + "'"
+ userNameIn + "'");
ResultSet rs = dbi
.singleQuery("SELECT * FROM bmcert WHERE username =" + "'"
+ userNameIn + "'");
try {
while (rs.next()) {
String name = rs.getString("username");
String cert = rs.getString("usercert");
System.out.println("User name is: " + name);
System.out.print("Cert is: " + cert);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}