Java相当于MD5CryptoServiceProvider computeHash& ToBase64String

时间:2014-08-22 13:48:07

标签: java c# encryption cryptography

我使用以下方式为C#中的字符串计算MD5哈希值:

var provider = new System.Security.Cryptography.MD5CryptoServiceProvider();
var bytes= Encoding.Unicode.GetBytes(value);
bytes = provider.ComputeHash(bytes);
return Convert.ToBase64String(bytes);

我需要在Java(Android)中进行相同的计算,以匹配使用上述方法生成的哈希。我尝试了以下但没有运气:

1

byte[] encoded = Base64.encodeBase64(str.getBytes());
return new String(encoded);

2

String digest = null;

MessageDigest md = MessageDigest.getInstance("MD5");
byte[] hash = md.digest(message.getBytes("UTF-8"));

//converting byte array to Hexadecimal String
StringBuilder sb = new StringBuilder(2*hash.length);
for(byte b : hash){
    sb.append(String.format("%02x", b&0xff));
}

digest = sb.toString();

3

String resultHash = null;
    try {
        MessageDigest md5 = MessageDigest.getInstance("MD5");

        byte[] result = new byte[md5.getDigestLength()];
        md5.reset();
        md5.update(buffer);
        result = md5.digest();

        StringBuffer buf = new StringBuffer(result.length * 2);

        for (int i = 0; i < result.length; i++) {
            int intVal = result[i] & 0xff;
            if (intVal < 0x10) {
                buf.append("0");
            }
            buf.append(Integer.toHexString(intVal));
        }

        resultHash = buf.toString();
    } catch (NoSuchAlgorithmException e) {
    }
    return resultHash;

修改

生成C#匹配值的Objective C方法是:

NSMutableString *encodedData = [NSMutableString string];

int i = 0, j = 0;

unsigned char char_array_3[3];

unsigned char char_array_4[5];



memset(char_array_3, 0, 3*sizeof(char));

memset(char_array_4, 0, 5*sizeof(char));



int length = [md5Data length];

char *bytes = (char*)[md5Data bytes];



while(length--) {

    char_array_3[i++] = *(bytes++);

    if (i == 3) {

        char_array_4[0] = kBase64Alphabet[(char_array_3[0] & 0xfc)>>2];

        char_array_4[1] = kBase64Alphabet[((char_array_3[0] &

                                            0x03) <<4) + ((char_array_3[1] & 0xf0) >>4)];

        char_array_4[2] = kBase64Alphabet[((char_array_3[1] &

                                            0x0f) <<2) + ((char_array_3[2] & 0xc0) >>6)];

        char_array_4[3] = kBase64Alphabet[char_array_3[2]&0x3f];



        [encodedData appendString:[NSString

                                   stringWithUTF8String:(const char*)char_array_4]];



        i = 0;

    }

}



if (i) {

    for(j=i; j<3; j++)

        char_array_3[j] = '\0';



    char_array_4[0] = kBase64Alphabet[(char_array_3[0] & 0xfc)>>2];

    char_array_4[1] = kBase64Alphabet[((char_array_3[0] & 0x03)

                                       <<4) + ((char_array_3[1] & 0xf0) >>4)];

    char_array_4[2] = kBase64Alphabet[((char_array_3[1] & 0x0f)

                                       <<2) + ((char_array_3[2] & 0xc0) >>6)];

    char_array_4[3] = kBase64Alphabet[char_array_3[2]&0x3f];



    char_array_4[i+1] = 0;

    [encodedData appendString:[NSString

                               stringWithUTF8String:(const char*)char_array_4]];



    while((i++<3))

        [encodedData appendString:[NSString stringWithUTF8String:"="]];

    }



    return encodedData;

}

示例

 `wattlebird` in C# -> `ixfbWnWq9QmLecMFCzaZcw==`
 `wattlebird` in java -> `GTd5lE58tKIlpdmppEVurw==`

2 个答案:

答案 0 :(得分:3)

Base64!= Hex。您可以使用Base64中的apache commons-codec

import org.apache.commons.codec.binary.Base64;

// ...

public static String hashValue(String value) {
    String digest = null;
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] hash = md.digest(value.getBytes("UTF-16LE")); // <-- note encoding
        return new String(Base64.encodeBase64(hash));
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return null;
}

答案 1 :(得分:1)

Base64不仅仅是十六进制字节的值。 (这是关于如何在Base64中编码的RFC规范RFC 4648

Android有一个内置版本,可以使用Base64编码android.util.Base64

MessageDigest md = MessageDigest.getInstance("MD5");
byte[] hash = md.digest(message.getBytes("UTF-8"));

String base64 = Base64.encodeToString(hash, Base64.DEFAULT);