C# 加密、解密函数

  1  #region ========加密========
  2 
  3         /// <summary>
  4         /// 加密
  5         /// </summary>
  6         /// <param name="Text"></param>
  7         /// <returns></returns>
  8         public static string Encrypt(string Text)
  9         {
 10             if (Text == null)
 11                 return Text;
 12             return Encrypt(Text, "123456789abcd");
 13         }
 14         /// <summary> 
 15         /// 加密数据 
 16         /// </summary> 
 17         /// <param name="Text"></param> 
 18         /// <param name="sKey"></param> 
 19         /// <returns></returns> 
 20         public static string Encrypt(string Text, string sKey)
 21         {
 22             DESCryptoServiceProvider des = new DESCryptoServiceProvider();
 23             byte[] inputByteArray;
 24             inputByteArray = Encoding.Default.GetBytes(Text);
 25             des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
 26             des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
 27             System.IO.MemoryStream ms = new System.IO.MemoryStream();
 28             CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
 29             cs.Write(inputByteArray, 0, inputByteArray.Length);
 30             cs.FlushFinalBlock();
 31             StringBuilder ret = new StringBuilder();
 32             foreach (byte b in ms.ToArray())
 33             {
 34                 ret.AppendFormat("{0:X2}", b);
 35             }
 36             return ret.ToString();
 37         }
 38 
 39         #endregion
 40 
 41         #region ========解密========
 42 
 43 
 44         /// <summary>
 45         /// 解密
 46         /// </summary>
 47         /// <param name="Text"></param>
 48         /// <returns></returns>
 49         public static string Decrypt(string Text)
 50         {
 51             if (Text == null)
 52                 return Text;
 53             return Decrypt(Text, "123456789abcd");
 54         }
 55         /// <summary> 
 56         /// 解密数据 
 57         /// </summary> 
 58         /// <param name="Text"></param> 
 59         /// <param name="sKey"></param> 
 60         /// <returns></returns> 
 61         public static string Decrypt(string Text, string sKey)
 62         {
 63             DESCryptoServiceProvider des = new DESCryptoServiceProvider();
 64             int len;
 65             len = Text.Length / 2;
 66             byte[] inputByteArray = new byte[len];
 67             int x, i;
 68             for (x = 0; x < len; x++)
 69             {
 70                 i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
 71                 inputByteArray[x] = (byte)i;
 72             }
 73             des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
 74             des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
 75             System.IO.MemoryStream ms = new System.IO.MemoryStream();
 76             CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
 77             cs.Write(inputByteArray, 0, inputByteArray.Length);
 78             cs.FlushFinalBlock();
 79             return Encoding.Default.GetString(ms.ToArray());
 80         } 98           197         #endregion 

 

转载于:https://www.cnblogs.com/gates/p/3456947.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值