gpt4 book ai didi

Java 相当于 MD5CryptoServiceProvider computeHash & ToBase64String

转载 作者:太空宇宙 更新时间:2023-11-03 21:28:33 25 4
gpt4 key购买 nike

我通过以下方式在 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==`

最佳答案

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

关于Java 相当于 MD5CryptoServiceProvider computeHash & ToBase64String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25448586/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com