1

I've been developing an Android App and in certain part of the app I need to calculate the MD5 of a certain string. I've been using the following code, but every now and then the output string if the byte it has to convert to String is lower than 10, then it will miss a 0 in the two byte representation:

    MessageDigest di = java.security.MessageDigest.getInstance("MD5");
    di.update(cadena.getBytes());
    byte mdi[] = di.digest();

    StringBuffer md5= new StringBuffer();
    for (byte b : mdi) {
        md5.append(Integer.toHexString(0xFF & b));
    }

For example, if I pass the string 109370 the MD5 it will have to return is 932ff0696b0434d7a83e1ff84fe298c5 but instead it calculates the 932ff0696b434d7a83e1ff84fe298c5.

That's because the byte array has a 4 and Integer.toHexString() is returning only 1 char array instead of two.

Any thought about how can I handle this?

Thanks!

4

3 回答 3

2

You can use a java.util.Formatter:

    Formatter fmt = new Formatter(md5);
    for (byte b : mdi) {
        fmt.format("%02x", b&0xff);
    }
    fmt.close();
于 2013-09-27T14:43:45.340 回答
2

下面是我正在使用的代码:

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


public class MD5Encode {
   private static String convertedToHex(byte[] data) {
    StringBuffer buf = new StringBuffer();

    for (int i = 0; i < data.length; i++) {
        int halfOfByte = (data[i] >>> 4) & 0x0F;
        int twoHalfBytes = 0;

        do {
            if ((0 <= halfOfByte) && (halfOfByte <= 9)) {
                buf.append((char) ('0' + halfOfByte));
            } else {
                buf.append((char) ('a' + (halfOfByte - 10)));
            }

            halfOfByte = data[i] & 0x0F;

        } while (twoHalfBytes++ < 1);
    }
    return buf.toString();
    }

    public static String MD5(String text) throws NoSuchAlgorithmException,
        UnsupportedEncodingException {
    MessageDigest md;
    md = MessageDigest.getInstance("MD5");
    byte[] md5 = new byte[64];
    md.update(text.getBytes("iso-8859-1"), 0, text.length());
    md5 = md.digest();
    return convertedToHex(md5);
   }
}

并以这种方式使用它:

MD5Encode.MD5("your string here")

希望对你有帮助 :)

于 2013-09-27T14:49:09.663 回答
0

用这个:

public String calculateMD5(String string) {
    StringBuilder result = new StringBuilder();
    try {
        MessageDigest m = MessageDigest.getInstance("MD5");
        m.update(string.getBytes("UTF8"));

        byte s[] = m.digest();

        for (int i = 0; i < s.length; i++) {
            result.append(Integer.toHexString((0x000000ff & s[i]) | 0xffffff00).substring(6));
        }
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException("Password hash is unsupported by device android implementation.", e);
    } catch (UnsupportedEncodingException e) {
        throw new IllegalStateException("Password hash is unsupported by device android implementation.", e);
    }
    return result.toString();
}
于 2013-09-27T14:42:19.947 回答