C#和Python的Base64和SHA256结果不同

时间:2018-12-29 17:50:12

标签: c# python base64 sha256

我是一个新手和初学者,因为我的英语不好,所以我会讲长话,请帮助我。

公钥是Base64解码和SHA256。
使用C#和Python(Python结果正确。)
为什么会得到不同的结果?

如果愿意,我希望通过 C#代码得到答案(我的英语很差。)
非常感谢。

相同的公钥MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0k9N59vMq/1BP6lwgyxVPeWj6EFTMW+wJqyl+vePi1vKzJBCXWPZ5Ls7PmNaTaAQ5TVC5WVoxveuvaYLGm514Y7EDTrL8BrPWcK73Gw7E/IQyfOC+/Mw6mBnANyWiVAu9qIi2/PWasA2J/XR97kfUfuM716NKUNzBmmju8pfyPu+ee9Zfh0fUclF0g48AJMZaw8g6SxiWnQ8XhABkMHXyqVio5cdt5omupDcZ17HHBXsP3KHNX5Tu8ZDrCbFH566p1WGjM6W2Wr2YffzC8WPyFTS/6eiAde5iVjS2VHqRkmgQvrH794kGUC7ZUitP7p6gYCvhdffqxLQslvga9cPNQIDAQAB

Base64解码==> SHA256

Python

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

结果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);
            }
        }

结果1eb61f3f380bccf54e61e05cdbe6e14c3871c6c827b33dc03b2cf47c1fc0df4c

0 个答案:

没有答案