Java equivalent to MD5CryptoServiceProvider computeHash &

本文关键字:amp computeHash MD5CryptoServiceProvider equivalent to Java | 更新日期: 2023-09-27 18:19:15

我用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==`

Java equivalent to MD5CryptoServiceProvider computeHash &

Base64 !你可以使用apache common -codec中的Base64

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;
}

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

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);