C#SHA-1与PHP SHA-1 …不同的结果?

我想从一个string计算一个SHA-1哈希,但是当我使用php的sha1函数计算string时,我得到了与在C#中尝试不同的东西。 我需要C#来计算相同的string作为PHP(因为PHP的string是由第三方计算,我不能修改)。 我如何让C#生成与PHP相同的散列? 谢谢!!!

string= s934kladfklada@a.com

C#代码(生成d32954053ee93985f5c3ca2583145668bb7ade86)

string encode = secretkey + email; UnicodeEncoding UE = new UnicodeEncoding(); byte[] HashValue, MessageBytes = UE.GetBytes(encode); SHA1Managed SHhash = new SHA1Managed(); string strHex = ""; HashValue = SHhash.ComputeHash(MessageBytes); foreach(byte b in HashValue) { strHex += String.Format("{0:x2}", b); } 

PHP代码(生成a9410edeaf75222d7b576c1b23ca0a9af0dffa98)

 sha1(); 

使用ASCIIEncoding而不是UnicodeEncoding。 PHP使用ASCII字符集进行散列计算。

在.NET中这种方法等同于在PHP中的sha1:

 string sha1Hash(string password) { return string.Join("", SHA1CryptoServiceProvider.Create().ComputeHash(Encoding.UTF8.GetBytes(password)).Select(x => x.ToString("x2"))); } 

我也有这个问题。 下面的代码将工作。

 string dataString = "string to hash"; SHA1 hash = SHA1CryptoServiceProvider.Create(); byte[] plainTextBytes = Encoding.ASCII.GetBytes(dataString); byte[] hashBytes = hash.ComputeHash(plainTextBytes); string localChecksum = BitConverter.ToString(hashBytes) .Replace("-", "").ToLowerInvariant(); 

有同样的问题。 这段代码适用于我:

 string encode = secretkey + email; SHA1 sha1 = SHA1CryptoServiceProvider.Create(); byte[] encodeBytes = Encoding.ASCII.GetBytes(encode); byte[] encodeHashedBytes = sha1.ComputeHash(passwordBytes); string pencodeHashed = BitConverter. ToString(encode HashedBytes).Replace("-", "").ToLowerInvariant(); 

FWIW,我在Java中遇到类似的问题。 事实certificate,我必须使用“UTF-8”编码在Java中生成与sha1函数在PHP 5.3.1(在XAMPP Vista上运行)中生成的相同的SHA1哈希值。

  private static String SHA1(final String text) throws NoSuchAlgorithmException, UnsupportedEncodingException { final MessageDigest md = MessageDigest.getInstance("SHA-1"); md.update(text.getBytes("UTF-8")); return new String(org.apache.commons.codec.binary.Hex.encodeHex(md.digest())); } 

尝试以下! 我认为这将会很好:

 public static string SHA1Encodeb64(string toEncrypt) { //Produce an array of bytes which is the SHA1 hash byte[] sha1Signature = new byte[40]; byte[] sha = System.Text.Encoding.Default.GetBytes(toEncrypt); SHA1 sha1 = SHA1Managed.Create(); sha1Signature = sha1.ComputeHash(sha); /** * The BASE64 encoding standard's 6-bit alphabet, from RFC 1521, * plus the padding character at the end. */ char[] Base64Chars = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', '=' }; //Algorithm to encode the SHA1 hash using Base64 StringBuilder sb = new StringBuilder(); int len = sha1Signature.Length; int i = 0; int ival; while (len >= 3) { ival = ((int)sha1Signature[i++] + 256) & 0xff; ival <<= 8; ival += ((int)sha1Signature[i++] + 256) & 0xff; ival <<= 8; ival += ((int)sha1Signature[i++] + 256) & 0xff; len -= 3; sb.Append(Base64Chars[(ival >> 18) & 63]); sb.Append(Base64Chars[(ival >> 12) & 63]); sb.Append(Base64Chars[(ival >> 6) & 63]); sb.Append(Base64Chars[ival & 63]); } switch (len) { case 0: // No pads needed. break; case 1: // Two more output bytes and two pads. ival = ((int)sha1Signature[i++] + 256) & 0xff; ival <<= 16; sb.Append(Base64Chars[(ival >> 18) & 63]); sb.Append(Base64Chars[(ival >> 12) & 63]); sb.Append(Base64Chars[64]); sb.Append(Base64Chars[64]); break; case 2: // Three more output bytes and one pad. ival = ((int)sha1Signature[i++] + 256) & 0xff; ival <<= 8; ival += ((int)sha1Signature[i] + 256) & 0xff; ival <<= 8; sb.Append(Base64Chars[(ival >> 18) & 63]); sb.Append(Base64Chars[(ival >> 12) & 63]); sb.Append(Base64Chars[(ival >> 6) & 63]); sb.Append(Base64Chars[64]); break; } //Encode the signature using Base64 string base64Sha1Signature = sb.ToString(); return base64Sha1Signature; }