在 Java 和 C# 中计算 SHA-1 哈希值
在 Java 和 C# 中计算 SHA-1 哈希值
我试图在 C# 应用程序中复制 Java 应用程序的逻辑。其中一部分涉及生成密码的 SHA-1 哈希值。不幸的是我无法从 Java 和 C# 获得相同的结果。
C# Output : 64 0a b2 ba e0 7b ed c4 c1 63 f6 79 a7 46 f7 ab 7f b5 d1 fa Java Output: 164 10a b2 ba e0 17b ed c4 c1 163 f6 179 a7 146 f7 ab 17f b5 d1 fa
为了尝试弄清楚发生了什么,我一直在 Eclipse 和 Visual Studio 中使用调试器。
1. Check values of byte[] key: Java: { 84, 101, 115, 116 } C# : { 84, 101, 115, 116 } 2. Check value of byte[] hash: Java: { 100 10 -78 -70 -32 123 ... } C# : { 100 10 78 186 224 123 ... }
我已经阅读了有关该主题的其他帖子,其中大部分涉及输入字符串编码,但这些似乎对我没有帮助。我的猜测是,这与有符号字节和无符号字节有关,但我在这条路上没有取得太大进展。任何帮助将不胜感激。
谢谢,
Karle
Java 版本:
public void testHash() {
String password = "Test";
byte[] key = password.getBytes();
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] hash = md.digest(key);
String result = "";
for ( byte b : hash ) {
result += Integer.toHexString(b + 256) + " ";
}
System.out.println(result);
}
C# 版本:
public void testHash() {
String password = "Test";
byte[] key = System.Text.Encoding.Default.GetBytes(password);
SHA1 sha1 = SHA1Managed.Create();
byte[] hash = sha1.ComputeHash(key);
String result;
foreach ( byte b in hash ) {
result += Convert.ToInt32(b).ToString("x2") + " ";
}
Console.WriteLine(result);
}
Calculating SHA-1 hashes in Java and C#
I'm trying to replicate the logic of a Java application within a C# application. Part of this involves generating an SHA-1 hash of a password. Unfortunately I can't get the same results from Java and C#.
C# Output : 64 0a b2 ba e0 7b ed c4 c1 63 f6 79 a7 46 f7 ab 7f b5 d1 fa Java Output: 164 10a b2 ba e0 17b ed c4 c1 163 f6 179 a7 146 f7 ab 17f b5 d1 fa
To try and figure out what is happening I've been using the Debugger in Eclipse and Visual Studio.
1. Check values of byte[] key: Java: { 84, 101, 115, 116 } C# : { 84, 101, 115, 116 } 2. Check value of byte[] hash: Java: { 100 10 -78 -70 -32 123 ... } C# : { 100 10 78 186 224 123 ... }
I've read the other posts on this topic, which largely refer to input string encoding, but these don't seem to have helped me. My guess would be that this is something to do with signed vs. unsigned bytes but I'm not making much progress down this track. Any help will be greatly appreciated.
Thanks,
Karle
Java Version:
public void testHash() {
String password = "Test";
byte[] key = password.getBytes();
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] hash = md.digest(key);
String result = "";
for ( byte b : hash ) {
result += Integer.toHexString(b + 256) + " ";
}
System.out.println(result);
}
C# Version:
public void testHash() {
String password = "Test";
byte[] key = System.Text.Encoding.Default.GetBytes(password);
SHA1 sha1 = SHA1Managed.Create();
byte[] hash = sha1.ComputeHash(key);
String result;
foreach ( byte b in hash ) {
result += Convert.ToInt32(b).ToString("x2") + " ";
}
Console.WriteLine(result);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在Java版本中,不要使用
b + 256
;相反,使用b & 255.
. SHA-1 部分很好,这只是打印输出的问题。 Java 的“byte
”类型是有符号的:它返回 -128 到 127 之间的值。要获取相应的无符号值,仅当该值为负数时,才必须添加 256。与 255 的按位 AND(这就是“
& 255
”的作用)可进行正确的转换,在二进制级别,该转换是将值截断为 8 个最低有效位。In the Java version, do not use
b + 256
; instead, useb & 255
. The SHA-1 part is fine, this is just a matter of printing the output. Java's "byte
" type is signed: it returns values between -128 and 127. To get the corresponding unsigned value, you must add 256 only if the value is negative.A bitwise AND with 255 (that's what "
& 255
" does) operates the proper conversion, which, at the binary level, is truncation of the value to its 8 least significant bits.你的问题和答案对我来说非常有用,但我注意到当密码具有字符“0”时生成的哈希代码是不同的,所以我改变了一些代码(在Java中)。
your question and the answer were very useful to me, but I noticed that when the password has the character "0" hash codes generated are different, so I changed a little the code (in Java).