Newer
Older
SensorHub / SensorHub.Servers / SM4 / SecurityUtils.cs
root on 17 Sep 2021 4 KB first commit
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace SensorHub.Servers.SM4
{
    class SecurityUtils
    {
        /// <summary>
        /// 加密响应
        /// </summary>
        /// <param name="actionInfo"></param>
        /// <param name="appSecret"></param>
        /// <returns></returns>
        public static String encodeResponse(String actionInfo, String appSecret)
        {
            try
            {
                Console.WriteLine("明文:{0}", actionInfo);
                //1.生成随机数
                String random = DesTools.generalStringToAscii(8) + DesTools.generalStringToAscii(8);
                Console.WriteLine("随机数:{0}", random);

                //2.生成过程密钥
                String processKey = DesTools.Encrypt3Des(random, appSecret);
                Console.WriteLine("过程秘钥:{0}", processKey);

                //3. 将actionInfo转换16进制后,补80
                String ciphertext = DesTools.padding80(DesTools.byteToHexStr(Encoding.UTF8.GetBytes(actionInfo)));
                Console.WriteLine("转换16进制后补80: {0}", ciphertext);

                //4. 将字符串编码成16进制数字,适用于所有字符(包括中文)
                byte[] bytes = Encoding.ASCII.GetBytes(ciphertext);
                ciphertext = BitConverter.ToString(bytes, 0, bytes.Length).Replace("-", "");
                Console.WriteLine("16进制字符串: {0}", ciphertext);
                // 加密
                ciphertext = DesTools.Encrypt3Des(ciphertext, processKey);
                Console.WriteLine("密文: {0}", ciphertext);
                // 最终生成密文
                ciphertext = random + ciphertext;
                Console.WriteLine("带随机数的密文: {0}", ciphertext);
                return ciphertext;
            }
            catch (Exception e)
            {
                Console.WriteLine("加密错误. ", e);
                return null;
            }
        }


        /// <summary>
        /// 创建签名
        /// </summary>
        /// <param name="appSecret"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static String createSignature(String appSecret, Dictionary<String, Object> parameters)
        {
            SortedDictionary<String, Object> sortedMap = new SortedDictionary<String, Object>(parameters);
            StringBuilder sb = new StringBuilder();
            foreach (var map in sortedMap)
            {
                if (map.Value != null && !"sign".Equals(map.Key))
                {
                    sb.Append(map.Key).Append("=").Append(map.Value).Append("&");
                }
            }

            sb.Append("key=").Append(appSecret);
            //算出摘要
            Console.WriteLine("签名原数据:{0}", sb.ToString());//.Replace(" ", ""));

            MD5 md5 = MD5.Create();
            byte[] s = md5.ComputeHash(Encoding.ASCII.GetBytes(sb.ToString()));

            String signature = DesTools.byteToHexStr(s).ToUpper();
            Console.WriteLine("签名:{0}", signature);
            return signature;
        }


        //public static void Main()
        //{
        //    //请求报文体数据
        //    String s = "{\n" +
        //            "    \"deviceType\": \"gasjc01\",\n" +
        //            "    \"tid\": \"312021160001\",\n" +
        //            "    \"imei\": \"864831053786797\",\n" +
        //            "    \"iccid\": \"89861119257011752675\",\n" +
        //            "    \"cnt\": \"1234567890123456\",\n" +
        //            "    \"mac\": \"5e33a6376025d3a87fcb49bb12ace534\"\n" +
        //            "  }";

        //    Console.WriteLine("s:" + s);
        //    //加密报文体
        //    String s1 = SecurityUtils.encodeResponse(s, "dc43576cd2a045dfb48c93212fdbfdff");
        //    Console.WriteLine("s1:" + s1);



        //    //签名数据
        //    Dictionary<String, Object> hashMap = new Dictionary<String, Object>();
        //    hashMap.Add("appId", "a9c92d1b8b72417285744962d06e145b");
        //    hashMap.Add("body", s1);
        //    hashMap.Add("msgId", "22222222222222");
        //    hashMap.Add("encodeMethod", "md5");
        //    hashMap.Add("signMethod", "md5");
        //    String s2 = SecurityUtils
        //            .createSignature("dc43576cd2a045dfb48c93212fdbfdff", hashMap);
        //    Console.WriteLine("s2:" + s2);

        //    Console.ReadKey();
        //}

    }
}