首页 > 编程语言 >C#各种加密方法,字典排序

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

时间:2024-11-04 13:59:30浏览次数:3  
标签:加密 string C# return result new byte 字典

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;
 }

 

标签:加密,string,C#,return,result,new,byte,字典
From: https://www.cnblogs.com/zyg316/p/18525080

相关文章