计算Java和C#SHA-1散列 [英] Calculating SHA-1 hashes in Java and C#
问题描述
计算SHA-1散列Java和C#
我想一个C#应用程序中复制一个Java应用程序的逻辑。这部分内容涉及到生成一个密码的SHA-1散列。不幸的是我无法从Java和C#一样的结果。
C#输出:64 0A B2 BA E0 7B版C4 C1 63 F6 79 A7 46 F7 AB 1408米B5 D1发
Java输出:164 10A B2 BA E0 17B版C4 C1 163 F6 179 A7 146 F7 AB 17F B5 D1发
要设法弄清楚发生了什么事我一直在使用在Eclipse和Visual Studio调试器。
$ b $
的Java:{84,101,115,116}
C#:{84,101,115,116}
$ b 1.检查的byte []键的值
的Java:字节[]哈希乙$ b 2.检查值{100 10 -78 -70 -32 123 ...}
C#:{100 10 78 186 224 123 ...}
我读过关于这个话题,这在很大程度上是指输入字符串编码的其他职位,但这些不似乎已经帮助了我。我的猜测是,这事做对签订无符号字节,但是,我不是做下来这条赛道多大进展。任何帮助将不胜感激。
谢谢,
Karle
Java版本:
公共无效testHash(){
字符串密码=测试;
的byte []键= password.getBytes();
消息摘要MD = MessageDigest.getInstance(SHA-1);
字节[] =哈希md.digest(键);
字符串结果=;
为(BYTE B:哈希){
结果+ = Integer.toHexString(B + 256)+;
}
的System.out.println(结果);
}
C#版本:
公共无效testHash(){
字符串密码=测试;
的byte []键= System.Text.Encoding.Default.GetBytes(密码);
SHA1 SHA1 = SHA1Managed.Create();
字节[] =哈希sha1.ComputeHash(键);
字符串结果;
的foreach(在散列字节二){
结果+ = Convert.ToInt32(二)的ToString(×2)+;
}
Console.WriteLine(结果);
}
在Java版本,不使用 b + 256
;相反,使用 B和; 255
。该SHA-1的部分是好的,这只是一个打印输出的问题。 Java的字节
类型是有符号:它-128到127之间的值返回,以获取相应的无符号数,则必须添加256的仅在的该值为负
位与255(这是&放大器; 255
一样)操作适当的转换,其中,在二进制级别,是价值的8个最低位显著截断。
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);
}
In the Java version, do not use b + 256
; instead, use b & 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.
这篇关于计算Java和C#SHA-1散列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!