user3112115
user3112115

Reputation: 715

MessageDigges md5 differs from the database md5 string

   MessageDigest md = MessageDigest.getInstance("MD5");
   String md5password = new String(md.digest("test".getBytes("UTF-8")), "UTF-8");

I have the same string "test" in my database, but here I get something different like this:

�k�F!�s��N�&'��

database:

098f6bcd4621d373cade4e832627b4f6

Upvotes: 0

Views: 47

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1499760

This is the problem:

new String(md.digest("test".getBytes("UTF-8")), "UTF-8");

You're trying to decode the result of the MD5 digest as if it were a UTF-8 string. It's not. It's not text at all - it's just binary data. What you're doing is like trying to load an image or music file as a string - it's just a bad idea.

It looks like your database is either storing it as binary data and showing you a hex representation, or storing it as hex. I suggest you do the same. There are lots of Stack Overflow questions about converting byte arrays to hex in Java, so that part should be simple.

Upvotes: 2

Related Questions