Different SHA1 Hash result between Java and C#

前端 未结 3 1556
心在旅途
心在旅途 2021-01-03 05:15

I\'ve a big problem. I using this C# function to encode my message:

byte[] buffer = Encoding.ASCII.GetBytes(file_or_text);
SHA1CryptoServiceProvider cryptoTr         


        
相关标签:
3条回答
  • 2021-01-03 05:48

    Try the following code:

    public static string Sha1encode(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;
        }
    
        string base64Sha1Signature = sb.ToString();
        return base64Sha1Signature;
    }
    
    0 讨论(0)
  • 2021-01-03 05:52

    The change to use ISO-8859-1 on the C# side is easy:

    byte[] buffer = Encoding.GetEncoding(28591).GetBytes(file_or_text);
    

    However, both this and ASCII will lose data if your text contains Unicode characters above U+00FF.

    Ideally if your source data is genuinely text, you should use an encoding which will cope with anything (e.g. UTF-8) and if your source data is actually binary, you shouldn't be going through text encoding at all.

    0 讨论(0)
  • 2021-01-03 05:58

    My guess is you seem to be comparing ASCII bytes to Latin1 bytes. Try switching

    md.update(text.getBytes("iso-8859-1"), 0, text.length());
    

    to this

    md.update(text.getBytes("ISO646-US"), 0, text.length());
    

    That might solve your problem.

    (Or switch C# to use Latin1)

    What is happening in your program your GetBytes method is returning different values for the same characters depending on encoding, so our nifty SHA1 hash algorithm is getting passed different parameters resulting in different return values.

    0 讨论(0)
提交回复
热议问题