Question

I am trying to convert a String to its MD5 representation with this code:

public static void main(String[] args) throws NoSuchAlgorithmException {
        String s = "oshai";
        MessageDigest m = MessageDigest.getInstance("MD5");
        m.update(s.getBytes(),0,s.length());
        String md5 = new BigInteger(1,m.digest()).toString(16);
        System.out.println(md5.length());
}

The returned String has add number of digits (31, so it can be an Hex number). What am I doing wrong?

Was it helpful?

Solution

You don't want to use BigInteger. Try a more traditional toHexString method..

public static void main(String[] args) throws NoSuchAlgorithmException {
        String s = "oshai";
        MessageDigest m = MessageDigest.getInstance("MD5");
        m.update(s.getBytes(),0,s.length());
        String string = toHexString(m.digest());
        System.out.println(string);
}

public static String toHexString(byte[] bytes) {
    StringBuilder sb = new StringBuilder();
    for(byte b : bytes) {
        sb.append(String.format("%02x", b));
    }
    return sb.toString();
}

OTHER TIPS

This method works for sure:

private String hashWithMD5(String text) throws UnsupportedEncodingException, NoSuchAlgorithmException {
    MessageDigest messageDigest = MessageDigest.getInstance("MD5");
    byte[] digest = messageDigest.digest(text.getBytes("UTF-8"));

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < digest.length; i++) {
        sb.append(Integer.toString((digest[i] & 0xff) + 0x100, 16).substring(1));
    }

    return sb.toString();
}

I have faced an error with missing "00" at the left side, while converting string to the encrypted format.

Normally you won't find the bug in your app by using the common md5 method.

So, please test your app with the string "sandeep" (I have used it because it has a "00" at left side).

This issue messed my hours and finally i found the following solution from a link.

"I had an error with md5 string with 00 at leftside, ie, a string “sandeep” converted to “DCF16D903E5890AABA465B0B1BA51F ” than the actual “00DCF16D903E5890AABA465B0B1BA51F

I ended up with this method, that work cool in my app."

public static  String md5(String inputString) {
    try {
        // Create MD5 Hash
        MessageDigest msgDigest = java.security.MessageDigest.getInstance("MD5");
        msgDigest.update(inputString.getBytes());
        byte msgDigestBytes[] = msgDigest.digest();

        // Create Hex String
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < msgDigestBytes.length; i++) {
            String h = Integer.toHexString(0xFF & msgDigestBytes[i]);
            while (h.length() < 2)
                h = "0" + h;
            hexString.append(h);
        }
        return hexString.toString();

    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "";
}

Ref:http://www.coderexception.com/CbHuB1uHPWxXUQXi/converting-string-to-md5-gives-add-number-of-digits

use this method :

public static String md5(String input) {

    String md5 = null;

    if (null == input)
        return null;

    try {

        // Create MessageDigest object for MD5
        MessageDigest digest = MessageDigest.getInstance("MD5");

        // Update input string in message digest
        digest.update(input.getBytes(), 0, input.length());

        // Converts message digest value in base 16 (hex)
        md5 = new BigInteger(1, digest.digest()).toString(16);

    } catch (NoSuchAlgorithmException e) {

        e.printStackTrace();
    }
    return md5;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top