The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Re: How to do MD5 encryption in JSP for password login and registration?

<%@page contentType="text/html; charset=iso-8859-1" language="java" import="java.security.MessageDigest"%>
<html>
<body>
This jsp code shows an example of a functioning md5 hash.<br>
The plaintext password is 'password' and is hard coded.<br>
The MD5 hash version of 'password' is '5f4dcc3b5aa765d61d8327deb882cf99'<br>
and is calculated each time the page is loaded.<br><br>
<%
// Set password string, and print it out
String passwd = "password";
out.println("Password is: " + passwd + ".<br>");
// Create a new instance of MessageDigest, using MD5. SHA and other
// digest algorithms are also available.
MessageDigest alg = MessageDigest.getInstance("MD5");
// Reset the digest, in case it's been used already during this section of code
// This probably isn't needed for pages of 210 simplicity
alg.reset();
// Calculate the md5 hash for the password. md5 operates on bytes, so give
// MessageDigest the byte verison of the string
alg.update(passwd.getBytes());
// Create a byte array from the string digest
byte[] digest = alg.digest();
// Convert the hash from whatever format it's in, to hex format
// which is the normal way to display and report md5 sums
// This is done byte by byte, and put into a StringBuffer
StringBuffer hashedpasswd = new StringBuffer();
String hx;
for (int i=0;i<digest.length;i++){
hx = Integer.toHexString(0xFF & digest[i]);
//0x03 is equal to 0x3, but we need 0x03 for our md5sum
if(hx.length() == 1){hx = "0" + hx;}
hashedpasswd.append(hx);
}
// Print out the string hex version of the md5 hash
out.println("MD5 version is: " + hashedpasswd.toString() + "<br>");
%>
</body>
</html>

i find this code, then i use this for my encryption with little modified, and in this tutorial said, this code is too old, and have some security issue, is there something wrong with this code? because i try it and it work well

i want to ask how about decrypt it?
i need to decrypt the password while my user forget the password, and i use it to send it for them