DavidYun

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  1 #region ===========================DES算法===================================
  2        
  3         private static string key = "key";
  4         /// <summary>
  5         /// 默认加密方法
  6         /// </summary>
  7         /// <param name="text"></param>
  8         /// <returns></returns>
  9         public static string DESEncrypt(string text)
 10         {
 11             return DESEncrypt(text, key);
 12         }
 13         /// <summary>
 14         /// DES加密方法
 15         /// </summary>
 16         /// <param name="text">明文</param>
 17         /// <param name="sKey">密钥</param>
 18         /// <returns>加密后的密文</returns>
 19         public static string DESEncrypt(string text,string sKey)
 20         {
 21             DESCryptoServiceProvider des = new DESCryptoServiceProvider();
 22             byte[] inputByteArray;
 23             inputByteArray = Encoding.Default.GetBytes(text);
 24             des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
 25             des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
 26             System.IO.MemoryStream ms = new System.IO.MemoryStream();
 27             CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
 28             cs.Write(inputByteArray, 0, inputByteArray.Length);
 29             cs.FlushFinalBlock();
 30             StringBuilder ret = new StringBuilder();
 31             foreach (byte b in ms.ToArray())
 32             {
 33                 ret.AppendFormat("{0:X2}", b);
 34             }
 35             ms.Dispose();
 36             cs.Dispose();
 37             return ret.ToString();
 38 
 39         }
 40         /// <summary>
 41         /// DES解密方法,默认方法
 42         /// </summary>
 43         /// <param name="text">待加密明文</param>
 44         /// <returns>加密后的密文</returns>
 45         public static string DESDecrypt(string text)
 46         {
 47             return DESDecrypt(text,key);
 48         }
 49         /// <summary>
 50         /// DES解密方法
 51         /// </summary>
 52         /// <param name="text">密文</param>
 53         /// <param name="sKey">密钥</param>
 54         /// <returns>解密后的明文</returns>
 55         public static string DESDecrypt(string text,string sKey)
 56         {
 57             DESCryptoServiceProvider des = new DESCryptoServiceProvider();
 58             int len;
 59             len = text.Length / 2;
 60             byte[] inputByteArray = new byte[len];
 61             int x, i;
 62             for (x = 0; x < len; x++)
 63             {
 64                 i = Convert.ToInt32(text.Substring(x * 2, 2), 16);
 65                 inputByteArray[x] = (byte)i;
 66             }
 67             try
 68             {
 69                 des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
 70                 des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
 71                 System.IO.MemoryStream ms = new System.IO.MemoryStream();
 72                 CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
 73                 cs.Write(inputByteArray, 0, inputByteArray.Length);
 74                 cs.FlushFinalBlock();
 75                 string estring = Encoding.Default.GetString(ms.ToArray());
 76                 ms.Dispose();
 77                 cs.Dispose();
 78                 return estring;
 79             }
 80             catch
 81             {
 82                 return "";
 83             }
 84 
 85         } 
 86         #endregion
 87 
 88         #region ==============================MD5算法==================================
 89         /// <summary>
 90         /// 使用MD5算法求Hash散列
 91         /// </summary>
 92         /// <param name="text">明文</param>
 93         /// <returns>散列值</returns>
 94         public static string MD5Encrypt(string text)
 95         {
 96             return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(text, "MD5");
 97         } 
 98         #endregion============================================================
 99 
100 
101         #region =============================SHA1==============================
102 
103         /// <summary>
104         /// 使用SHA1算法求Hash散列
105         /// </summary>
106         /// <param name="text">明文</param>
107         /// <returns>散列值</returns>
108         public static string SHA1Encrypt(string text)
109         {
110             return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(text, "SHA1");
111         } 
112         #endregion=============================================================
113 
114 
115 
116     }

 

posted on 2013-02-20 17:44  David.Yun  阅读(5729)  评论(0编辑  收藏  举报