c#与python webapi加密实现

项目背景

需要在企业内部对AD(域控)和Exchange实现日常的自动化操作

采用.net webapi与AD交互

webapi加密原理

加密传输原理图:

image

c#代码

static void Main()
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            var ts_str=Convert.ToInt64(ts.TotalSeconds).ToString();
            var strTimestamp = "1482805782";
            var WEBTOKEN = "5[k6yuUOBph1t!tQ";
            var strNonce = "531bf49ccbdc11e69ea8005056c00008";
            //step1:字典序排序
            string[] array = new[] { WEBTOKEN, strTimestamp, strNonce };
            Popsort(array);
            string array_str = string.Empty;
            foreach (string str in array)
            {
                array_str =array_str+ str;
            }


            //            step2:sha1加密
            SHA1 sha1 = new SHA1CryptoServiceProvider();

            byte[] bytes_old_string = UTF8Encoding.Default.GetBytes(string.Concat(array));

            byte[] bytes_new_string = sha1.ComputeHash(bytes_old_string);

            string new_string = BitConverter.ToString(bytes_new_string);

            new_string = new_string.Replace("-", "").ToLower();



//            var buffer = Encoding.UTF8.GetBytes(array_str);
//            var data = SHA1.Create().ComputeHash(buffer);
//
//            var sb = new StringBuilder();
//            foreach (var t in data)
//            {
//                sb.Append(t.ToString("X2"));
//            }
            Console.WriteLine(new_string);

            Console.ReadKey();
        }
// c# sort字符串排序是按照字符大小排序,这里采用冒泡排序算法,按照assci码排序
        public static Array Popsort(string[] arr)
        {
            string temp = string.Empty;
            //冒泡排序
            for (int i = arr.Length; i > 0; i--)
            {
                for (int j = 0; j < i - 1; j++)
                {
                    char[] arr_char1 = arr[j].ToCharArray();
                    char[] arr_char2 = arr[j + 1].ToCharArray();
                    int size = arr_char1.Length;
                    int m = 0;
                    for (m = 0; m < size; m++)
                    {
                        if (arr_char1[m]>arr_char2[m])
                        {
                            temp = arr[j];
                            arr[j] = arr[j + 1];
                            arr[j + 1] = temp;
                            break;
                        }
                        else if (arr_char1[m] < arr_char2[m])
                        {
                            break;
                        }
                    }
                }
            }
            return arr;
        }

python代码

import hashlib
import datetime
import time
import uuid

WEBAPI_TOKEN = "5[k6yuUOBph1t!tQ"


endtime = datetime.datetime.now()

starttime = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")

timeArray = time.strptime(starttime, "%Y-%m-%d %H:%M:%S")
timestamp = int(time.mktime(timeArray))
timestamp_str = str(timestamp)
print(timestamp_str)
nonce = uuid.uuid1().hex
print(nonce)
token = WEBAPI_TOKEN
tmp_list = [token,"1482805782","531bf49ccbdc11e69ea8005056c00008"]
tmp_list.sort()
tmp_str = "%s%s%s" % tuple(tmp_list)
m = hashlib.sha1(tmp_str.encode('utf-8')).hexdigest()
print(m)

以上代码进行过验证,但是没有进行web化编程,可以参考我们的另外一篇博客:python3 api加密

发表评论

电子邮件地址不会被公开。 必填项已用*标注