Java MD5哈希不匹配C#MD5哈希 [英] Java MD5 hashing not matching C# MD5 hashing
问题描述
我对加密/散列知之甚少。
我必须加密加密密钥。在Java中的例子是这样的...
String encryptionKey =test;
MessageDigest messageDigest = MessageDigest.getInstance(MD5);
messageDigest.update(encryptionKey.getBytes(UTF-8),0,encryptionKey.length());
byte [] encryptionKeyBytes = messageDigest.digest();
现在纠正我,如果我错了,但上面的代码散列字符串与MD5算法。
我想在C#中对相同的字符串进行哈希处理时得到相同的结果。
我当前的C#代码看起来像像这样......
string encryptionKey =test;
var md5 = MD5.Create();
var keyBytes = Encoding.UTF8.GetBytes(encryptionKey);
byte [] encryptionKeyBytes = md5.ComputeHash(keyBytes);
但结束字节结果不匹配。
Java获取...
[0] 9
[1] -113
[ 2] 107
[3] -51
[4] 70
[5] 33
[6] -45
[7] 115
[ 8] -54
[9] -34
[10] 78
[11] -125
[12] 38
[13] 39
[14] -76
[15] -10
C# p>
[0] 9字节
[1] 143字节
[2] 107字节
[ 3] 205字节
[4] 70字节
[5] 33字节
[6] 211字节
[7] 115字节
[8] 202字节
[9] 222字节
[10] 78字节
[11] 131字节
[12] 38字节
[13] 39字节
[14 ] 180字节
[15] 246字节
我需要我的C#代码才能得到相同的结果作为Java代码(而不是其他方式),任何想法?
谢谢。
实际上,结果完全相同。像其他整型类型一样,一个字节值可能被解释为有符号或无符号。例如,如果解释为无符号, 因此,这种不一致似乎是由于您的值被格式化为用Java签名但在C#中未签名。如果你想在C#中得到带符号的字节,你可以使用: 但是,请注意,这不仅仅是一种格式问题,只会在您显示值时出现。保存到文件时,两个结果应该是相同的。 I know very little about encryption/hashing. I have to hash an encryption key. The example in Java is like this... Now correct me if I'm wrong, but the above code hashes the string with the MD5 algorithm. And I want the same result when I hash the same string in C#. My current C# code looks like this... But the end byte results do not match. Java gets... C# gets... I need my C# code to get the same result as the Java code (not the other way around), any ideas? Thank you. Actually, the results are identical. Like other integral types, a byte value may be interpreted as either signed or unsigned. For example, Thus, the disparity seems to be caused by your values being formatted as signed in Java but unsigned in C#. If you want to get signed bytes in C#, you can use: However, be careful that this is not merely a formatting issue that only arises when you display your values. When saved to file, both results should be identical. 这篇关于Java MD5哈希不匹配C#MD5哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! 10001111
将对应于 143
(您的第二个C#值)。但是,如果解释为签名(使用二补),其值将为 -113
(您的第二个Java值)。
sbyte [] encryptionKeyBytesSigned =
encryptionKeyBytes.Select (b =>(sbyte)b).ToArray();
String encryptionKey = "test";
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
messageDigest.update(encryptionKey.getBytes("UTF-8"), 0, encryptionKey.length());
byte[] encryptionKeyBytes = messageDigest.digest();
string encryptionKey = "test";
var md5 = MD5.Create();
var keyBytes = Encoding.UTF8.GetBytes(encryptionKey);
byte[] encryptionKeyBytes = md5.ComputeHash(keyBytes);
[0] 9
[1] -113
[2] 107
[3] -51
[4] 70
[5] 33
[6] -45
[7] 115
[8] -54
[9] -34
[10] 78
[11] -125
[12] 38
[13] 39
[14] -76
[15] -10
[0] 9 byte
[1] 143 byte
[2] 107 byte
[3] 205 byte
[4] 70 byte
[5] 33 byte
[6] 211 byte
[7] 115 byte
[8] 202 byte
[9] 222 byte
[10] 78 byte
[11] 131 byte
[12] 38 byte
[13] 39 byte
[14] 180 byte
[15] 246 byte
10001111
would correspond to 143
(your second C# value) if interpreted as unsigned. However, if interpreted as signed (using two’s complement), its value would be -113
(your second Java value).sbyte[] encryptionKeyBytesSigned =
encryptionKeyBytes.Select(b => (sbyte)b).ToArray();