Base64 and SHA256 results of C# and Python are different

Viewed 533

I'm a newcomer and beginner, because my English is not good, I will make a long story short, please help me.

A public key is Base64 decode and SHA256.
Use C# and Python (Python result is correct.)
Why do I get different results?

If you like, I hope to get the answer by C# code(my English is very poor.)
Thank you very much.

Same public key : MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0k9N59vMq/1BP6lwgyxVPeWj6EFTMW+wJqyl+vePi1vKzJBCXWPZ5Ls7PmNaTaAQ5TVC5WVoxveuvaYLGm514Y7EDTrL8BrPWcK73Gw7E/IQyfOC+/Mw6mBnANyWiVAu9qIi2/PWasA2J/XR97kfUfuM716NKUNzBmmju8pfyPu+ee9Zfh0fUclF0g48AJMZaw8g6SxiWnQ8XhABkMHXyqVio5cdt5omupDcZ17HHBXsP3KHNX5Tu8ZDrCbFH566p1WGjM6W2Wr2YffzC8WPyFTS/6eiAde5iVjS2VHqRkmgQvrH794kGUC7ZUitP7p6gYCvhdffqxLQslvga9cPNQIDAQAB

Base64 decode ==> SHA256

Python:

pubkey_der = base64.b64decode(pubKey)  
sha = hashlib.sha256(pubkey_der).hexdigest()
for i in sha:
    print(i),
print('\n'),

result:890e947269d4e6d9c73883157f65aa6f12e20ad0a05ff518b5f9cb43cea28b89

C#:

private void button_go2_Click(object sender, EventArgs e)
{
   string b64 = DecodeBase64(pubKey);
   string result = GetSHA256HashFromString(b64);
}


 private static string DecodeBase64(string pubKey)
        {
            string decode = string.Empty;
            byte[] bytes = Convert.FromBase64String(pubKey);
            decode = Encoding.UTF8.GetString(bytes);
            return decode; 
        }


 public string GetSHA256HashFromString(string strData)
        {
            byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(strData);
            try
            {
                SHA256 sha256 = new SHA256CryptoServiceProvider();
                byte[] retVal = sha256.ComputeHash(bytValue);
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < retVal.Length; i++)
                {
                    sb.Append(retVal[i].ToString("x2"));
                }
                return sb.ToString();
            }
            catch (Exception ex)
            {
                throw new Exception("GetSHA256HashFromString() fail,error:" + ex.Message);
            }
        }

result:1eb61f3f380bccf54e61e05cdbe6e14c3871c6c827b33dc03b2cf47c1fc0df4c

0 Answers
Related