Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Encrypting passwords with MD5

843810Apr 17 2003 — edited Apr 24 2003
Hi!

I have to save encrypted passwords and therefore I want (must?) convert them to String. byte[] -> String
In order to compare them again I have to re-convert them from String to byte[], don't I...

The problem is that it doesn't work. Please tell me what is wrong in this code:

---Encryption---

private byte[] encrypt(String s) {
byte[] sbe = null;
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(s.getBytes());
sbe = md.digest();

pw = sbe;
}
catch (NoSuchAlgorithmException ex) {}
return sbe;
}

----------------

where s is the password-string and byte[] sbe the encrypted password.

So how do i convert now sbe to a String I can save in a database or textfile and then re-convert it to a byte I can compare with another byte[].

---Comparision---

byte[] b1 = txtEncrypted.getText().getBytes();
byte[] b2;

try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(txtCompare.getText().getBytes());
b2 = md.digest();

if (md.isEqual(b1, b2)) {
monitor.append("Correct password!\n");
}
else {
monitor.append("Wrong password...\n");
}

----------------

where b2 is the "new" encrypted password which should be compared with the "old" stored one.

I hope you can help me and I'm sorry for my bad English... ;)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 22 2003
Added on Apr 17 2003
3 comments
299 views