troubleshooting Question

Java: password entered is not converting properly

Avatar of isames
isames asked on
3 Comments1 Solution85 ViewsLast Modified:
So my instructor gave us this code to convert the password entered by the user to a series of letters and numbers. For example, if I pass in "alphabet soup" , then it should return 108de81c31bf9c622f76876b74e9285f

To make sure I'm passing in the correct password, which is "alphabet soup" , I tested returning the variables original and userpassword. They both displayed "alphabet soup"
however when I returned the sb.toString(); I get 6cfa0a0071bb0f00ad9db9f5b8240d61 instead of 108de81c31bf9c622f76876b74e9285f

Also, if I manually type "alphabet soup" into the code below for the original variable, sb.toString(); returns the correct value of 108de81c31bf9c622f76876b74e9285f.

Why am I getting the wrong value when I pass the value in via the parameter, even though everything I see seems to be right?

public static String MD5Digest(String userPassword)throws Exception{
        String original = userPassword;  //Replace "password" with the actual password inputted by the user
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digest = md.digest();
        StringBuffer sb = new StringBuffer();
        for (byte b : digest) {
            sb.append(String.format("%02x", b & 0xff));
            //System.out.println("original:" + userPassword);
            //System.out.println("digested:" + sb.toString());
        return sb.toString(); 
        //return userPassword;
        //return original;

Open in new window

Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros