troubleshooting Question

Java: password entered is not converting properly

Avatar of isames
isames asked on
Java
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");
        md.update(original.getBytes());
        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;
    }
}
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
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