c# 签名算法(与java一致)

static string HMACSHA256(string inputText,string appkey)
        {
            //string signature = null;
            //using (var hmacsha256 = new HMACSHA256(keyBytes))
            //{
            //    byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
            //    for (int i = 0; i < hashmessage.Length; i++)
            //    {
            //        signature += hashmessage[i].ToString("x2");
            //    }
            //}
            ////var hashmessage = hmacsha256.ComputeHash(messageBytes);
            //return signature;
            var encoding = new System.Text.ASCIIEncoding();
            var messageBytes = encoding.GetBytes(inputText);
            var keyBytes = encoding.GetBytes(appkey);
             var hmacsha256 = new HMACSHA256(keyBytes);
            var hashmessage = hmacsha256.ComputeHash(messageBytes);
            string byte2String = null;
            for (int i = 0; i < hashmessage.Length; i++)
            {
                byte2String += hashmessage[i].ToString("x2");
            }
            return byte2String;
        }
        public static string MD5(string inputText)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] fromData = System.Text.Encoding.UTF8.GetBytes(inputText);
            byte[] targetData = md5.ComputeHash(fromData);
            string byte2String = null;

            for (int i = 0; i < targetData.Length; i++)
            {
                byte2String += targetData[i].ToString("x2");
            }
            return byte2String.ToUpper();
        }

以上代码经过与java接口验证过,备忘!

posted @ 2023-01-04 10:51  立扬  阅读(269)  评论(0)    收藏  举报
……