C#各種加密方法,字典排序

小飛發表於2024-11-04

1、字串加密

 /// <summary>
 /// 獲取MD5加密字串(type:0大寫,1小寫)
 /// </summary>
 /// <param name="content">加密內容</param>
 /// <param name="type">返回格式(0大寫,1小寫)</param>
 /// <returns></returns>
 public static string MD5Encrypt(string content, int type)
 {
     string result = string.Empty;
     try
     {
         if (string.IsNullOrEmpty(content))
             return result;
         MD5 md5 = MD5.Create();
         byte[] inputBytes = System.Text.Encoding.UTF8.GetBytes(content);
         byte[] hashBytes = md5.ComputeHash(inputBytes);
         result = BitConverter.ToString(hashBytes);
         result = type == 0 ? result.Replace("-", "").ToUpper() : result.Replace("-", "").ToLower();
         return result;
     }
     catch (Exception)
     {
         return result;
     }
 }

 /// <summary>
 /// SHA256簽名加密
 /// </summary>
 /// <param name="data"></param>
 /// <returns></returns>
 public static string sha256(string data)
 {
     byte[] bytes = Encoding.UTF8.GetBytes(data);
     byte[] hash = SHA256Managed.Create().ComputeHash(bytes);

     StringBuilder builder = new StringBuilder();
     for (int i = 0; i < hash.Length; i++)
     {
         builder.Append(hash[i].ToString("X2"));
     }
     return builder.ToString().ToLower(); ;
 }
 /// <summary>
 /// HMAC-SHA256加密
 /// </summary>
 /// <param name="message">加密的字串</param>
 /// <param name="secret">加密秘鑰</param>
 /// <returns></returns>
 public static string HMAC_SHA256(string message, string secret)
 {
     var encoding = new System.Text.UTF8Encoding();
     byte[] keyByte = encoding.GetBytes(secret);
     byte[] messageBytes = encoding.GetBytes(message);
     using (var hmacsha256 = new HMACSHA256(keyByte))
     {
         byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
         StringBuilder builder = new StringBuilder();
         for (int i = 0; i < hashmessage.Length; i++)
         {
             builder.Append(hashmessage[i].ToString("x2"));
         }
         return builder.ToString();
     }
 }
 /// <summary>
 /// HMAC-SHA256加密
 /// </summary>
 /// <param name="message">加密的字串</param>
 /// <param name="secret">加密秘鑰</param>
 /// <returns></returns>
 public static string HMAC_SHA256Base64(string message, string secret)
 {
     var encoding = new System.Text.UTF8Encoding();
     byte[] keyByte = Convert.FromBase64String(secret);
     byte[] messageBytes = encoding.GetBytes(message);
     using (var hmacsha256 = new HMACSHA256(keyByte))
     {
         byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
         StringBuilder builder = new StringBuilder();
         for (int i = 0; i < hashmessage.Length; i++)
         {
             builder.Append(hashmessage[i].ToString("x2"));
         }
         return builder.ToString();
     }
 }
 /// <summary>
 /// RSA公鑰加密
 /// </summary>
 /// <param name="content">加密的內容</param>
 /// <param name="publickey">公鑰</param>
 /// <returns>加密後的密文</returns>
 public static string RsaEncrypt(string content, string publickey)
 {
     RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
     byte[] cipherbytes;
     rsa.FromXmlString(publickey);
     cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(content), false);
     return Convert.ToBase64String(cipherbytes);
 }

 /// <summary>
 /// AES加密(加密模式:CBC)模式有多種,可根據實際需求選擇,此處列舉CBC
 /// </summary>
 /// <param name="toEncrypt">加密字元</param>
 /// <param name="secret">金鑰</param>
 /// <returns></returns>
 public static string DHEncrypt(string toEncrypt, string secret)
 {
     byte[] keyArray = UTF8Encoding.UTF8.GetBytes(secret);
     byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
     RijndaelManaged rDel = new RijndaelManaged();
     rDel.Key = keyArray;
//以下4項根據實際需求配置 rDel.Mode
= CipherMode.CBC; rDel.Padding = PaddingMode.PKCS7; rDel.BlockSize = 128; rDel.IV = keyArray.Skip(0).Take(16).ToArray();
//以上4項根據實際需求配置
ICryptoTransform cTransform
= rDel.CreateEncryptor(); byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return Convert.ToBase64String(resultArray, 0, resultArray.Length); }

2、對Dictionary字典進行ASCII排序

 /// <summary>
 /// 對Dictionary字典進行ASCII排序
 /// </summary>
 /// <param name="paramsMap"></param>
 /// <returns></returns>
 public static String getParamSrc(Dictionary<string, string> paramsMap)
 {
     //var vDic = from objDic in paramsMap orderby objDic.Key ascending select objDic;
     var keys = paramsMap.Keys.ToArray();
     Array.Sort(keys, string.CompareOrdinal);
     StringBuilder str = new StringBuilder();
     foreach (var item in keys)
     {
         str.Append(item + "=" + paramsMap[item] + "&");
     }
     //foreach (KeyValuePair<string, string> kv in vDic)
     //{
     //    string pkey = kv.Key;
     //    string pvalue = kv.Value;
     //    str.Append(pkey + "=" + pvalue + "&");
     //}
     String result = str.ToString().Substring(0, str.ToString().Length - 1);
     return result;
 }

相關文章