zoukankan      html  css  js  c++  java
  • MD5 以key和iv保存 FormsAuthentication.HashPasswordForStoringInConfigFile过时的问题

     1 //旧方法的写法
     2  #region ========加密========
     3         /// <summary>
     4         /// 加密
     5         /// </summary>
     6         /// <param name="Text"></param>
     7         /// <returns></returns>
     8         public static string Encrypt(string Text)
     9         {
    10             return Encrypt(Text, DESKey);
    11         }
    12         /// <summary> 
    13         /// 加密数据 
    14         /// </summary> 
    15         /// <param name="Text"></param> 
    16         /// <param name="sKey"></param> 
    17         /// <returns></returns> 
    18         public static string Encrypt(string Text, string sKey)
    19         {
    20             DESCryptoServiceProvider des = new DESCryptoServiceProvider();
    21             byte[] inputByteArray;
    22             inputByteArray = Encoding.Default.GetBytes(Text);
    23             des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
    24             des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
    25             System.IO.MemoryStream ms = new System.IO.MemoryStream();
    26             CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
    27             cs.Write(inputByteArray, 0, inputByteArray.Length);
    28             cs.FlushFinalBlock();
    29             StringBuilder ret = new StringBuilder();
    30             foreach (byte b in ms.ToArray())
    31             {
    32                 ret.AppendFormat("{0:X2}", b);
    33             }
    34             return ret.ToString();
    35         }
    36 
    37         #endregion
    38 
    39         #region ========解密========
    40         /// <summary>
    41         /// 解密
    42         /// </summary>
    43         /// <param name="Text"></param>
    44         /// <returns></returns>
    45         public static string Decrypt(string Text)
    46         {
    47             if (!string.IsNullOrEmpty(Text))
    48             {
    49                 return Decrypt(Text, DESKey);
    50             }
    51             else
    52             {
    53                 return "";
    54             }
    55         }
    56         /// <summary> 
    57         /// 解密数据 
    58         /// </summary> 
    59         /// <param name="Text"></param> 
    60         /// <param name="sKey"></param> 
    61         /// <returns></returns> 
    62         public static string Decrypt(string Text, string sKey)
    63         {
    64             DESCryptoServiceProvider des = new DESCryptoServiceProvider();
    65             int len;
    66             len = Text.Length / 2;
    67             byte[] inputByteArray = new byte[len];
    68             int x, i;
    69             for (x = 0; x < len; x++)
    70             {
    71                 i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
    72                 inputByteArray[x] = (byte)i;
    73             }
    74             des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
    75             des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
    76             System.IO.MemoryStream ms = new System.IO.MemoryStream();
    77             CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
    78             cs.Write(inputByteArray, 0, inputByteArray.Length);
    79             cs.FlushFinalBlock();
    80             return Encoding.Default.GetString(ms.ToArray());
    81         }
    82 
    83         #endregion
    View Code
     1 //新的写法
     2 
     3         #region ========加密========
     4 
     5         /// <summary>
     6         /// 加密
     7         /// </summary>
     8         /// <param name="Text"></param>
     9         /// <returns></returns>
    10         public static string Encrypt(string Text)
    11         {
    12             if (Text.Trim() == "")
    13                 return "";
    14             else
    15                 return Encrypt(Text, "passkey");
    16         }
    17         /// <summary> 
    18         /// 加密数据 
    19         /// </summary> 
    20         /// <param name="Text"></param> 
    21         /// <param name="sKey"></param> 
    22         /// <returns></returns> 
    23         public static string Encrypt(string Text, string sKey)
    24         {
    25             DESCryptoServiceProvider des = new DESCryptoServiceProvider();
    26             byte[] inputByteArray;
    27             inputByteArray = Encoding.Default.GetBytes(Text);
    28             des.Key = ASCIIEncoding.ASCII.GetBytes(MD5(sKey).Substring(0, 8));
    29             des.IV = ASCIIEncoding.ASCII.GetBytes(MD5(sKey).Substring(0, 8));
    30             System.IO.MemoryStream ms = new System.IO.MemoryStream();
    31             CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
    32             cs.Write(inputByteArray, 0, inputByteArray.Length);
    33             cs.FlushFinalBlock();
    34             StringBuilder ret = new StringBuilder();
    35             foreach (byte b in ms.ToArray())
    36             {
    37                 ret.AppendFormat("{0:X2}", b);
    38             }
    39             return ret.ToString();
    40         }
    41 
    42         #endregion
    43 
    44         #region ========解密========
    45 
    46 
    47         /// <summary>
    48         /// 解密
    49         /// </summary>
    50         /// <param name="Text"></param>
    51         /// <returns></returns>
    52         public static string Decrypt(string Text)
    53         {
    54             if (Text.Trim() == "")
    55                 return "";
    56             else
    57                 return Decrypt(Text, "passkey");
    58         }
    59         /// <summary> 
    60         /// 解密数据 
    61         /// </summary> 
    62         /// <param name="Text"></param> 
    63         /// <param name="sKey"></param> 
    64         /// <returns></returns> 
    65         public static string Decrypt(string Text, string sKey)
    66         {
    67             DESCryptoServiceProvider des = new DESCryptoServiceProvider();
    68             int len;
    69             len = Text.Length / 2;
    70             byte[] inputByteArray = new byte[len];
    71             int x, i;
    72             for (x = 0; x < len; x++)
    73             {
    74                 i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
    75                 inputByteArray[x] = (byte)i;
    76             }
    77             des.Key = ASCIIEncoding.ASCII.GetBytes(MD5(sKey).Substring(0, 8));
    78             des.IV = ASCIIEncoding.ASCII.GetBytes(MD5(sKey).Substring(0, 8));
    79             System.IO.MemoryStream ms = new System.IO.MemoryStream();
    80             CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
    81             cs.Write(inputByteArray, 0, inputByteArray.Length);
    82             cs.FlushFinalBlock();
    83             return Encoding.Default.GetString(ms.ToArray());
    84         }
    85 
    86         public static string MD5(string str)
    87         {
    88             //微软md5方法参考return FormsAuthentication.HashPasswordForStoringInConfigFile(str, "md5");
    89             byte[] b = Encoding.Default.GetBytes(str);
    90             b = new MD5CryptoServiceProvider().ComputeHash(b);
    91             string ret = "";
    92             for (int i = 0; i < b.Length; i++)
    93                 ret += b[i].ToString("X").PadLeft(2, '0');
    94             return ret;
    95         }
    96  
    97         #endregion       
    View Code
  • 相关阅读:
    O(n^2)的排序方法
    99乘法表
    excel 转 csv
    批量关闭 excel
    tomcat 加入服务
    文件打包 zip
    字符串转换
    List数组种删除数据
    mybatis 批量上传
    sql server 查询表字段及类型
  • 原文地址:https://www.cnblogs.com/Loners/p/12264962.html
Copyright © 2011-2022 走看看