I use MD5 algorithm to hash passwords with salt. After registering a user, I store both hashed password and salt in my SQL database. Unfortunately, when I retrieve them, later, so as to compare them with the user's input I address a problem with the encoding of salt. I store salt as VARCHAR(16) and when I try to retrieve it from my DataBase, it converts in a different form. This is my code :
ResultSet rs = stmt.executeQuery("SELECT * FROM users");
String DB_salt1 = rs.getString("Salt");
byte [] DB_salt = DB_salt1.getBytes();
I used some System.out.println();
functions in order to locate the problem and I found out that byte [] DB_salt = DB_salt1.getBytes();
outputs the wrong salt, while String DB_salt1 = rs.getString("Salt");
outputs the proper one. My guess is that it happens byte loss.
EDIT: To clarify, the proper salt and the one stored in my DataBase, let's say is [B@4e25154. String DB_salt1 = rs.getString("Salt");
stores this salt; the proper one. But when byte [] DB_salt = DB_salt1.getBytes();
is executed, it outputs a different salt, which ruins the whole process.