C# JSON参数名按ASCII码排序、MD5加密、DES解密

  public IDictionary asciiOrder(IDictionary sArray)
        {

            IDictionary asciiDic = new Dictionary();
            string[] arrKeys = sArray.Keys.ToArray();
            Array.Sort(arrKeys);
            foreach (var key in arrKeys)
            {
                //判断是否为空,为空的数据剔除掉
                if (!string.IsNullOrWhiteSpace(sArray[key]))
                {
                    string value = sArray[key];
                    asciiDic.Add(key, value);
                }
            }
            return asciiDic;
        }

MD5加密:

        public string GetMD5Hash(string input)
        {
            using (MD5 md5 = MD5.Create())//创建一个MD5实例
            {
                byte[] inputBytes = Encoding.UTF8.GetBytes(input);//将输入字符串转换为字节数组
                byte[] hashBytes = md5.ComputeHash(inputBytes);//计算输入字节数组的MD5哈希值

                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < hashBytes.Length; i++)
                {
                    sb.Append(hashBytes[i].ToString("x2"));
                }
                return sb.ToString();
            }
        }

DES解密:我这边的密钥是包含负数,所以用sbyte数组

 public string DecryptDES(string encryptedText, sbyte[] key)
        {
            try
            {
                byte[] encryptedBytes = Convert.FromBase64String(encryptedText);
                byte[] byteKey = new byte[key.Length];
                Buffer.BlockCopy(key, 0, byteKey, 0, key.Length);

                using (TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider())
                {
                    des.Mode = CipherMode.ECB;
                    des.Padding = PaddingMode.PKCS7;
                    des.Key = byteKey;

                    using (ICryptoTransform decryptor = des.CreateDecryptor())
                    {
                        byte[] decryptedBytes = decryptor.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length);
                        return Encoding.UTF8.GetString(decryptedBytes);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("DES解密出错了:" + ex.Message);
                return "";
            }

        }

你可能感兴趣的:(c#,json)