zoukankan      html  css  js  c++  java
  • 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 
  • 相关阅读:
    IOS编程教程(八):在你的应用程序添加启动画面
    IOS编程浅蓝教程(二) HelloWrld! 建立你的第一个iPhone应用程序
    IOS编程浅蓝教程:锲子
    第四章 Spring.Net 如何管理您的类___IObjectPostProcessor接口
    第一章 Spring.Net介绍
    第二章 控制反转和依赖注入
    第三章 Spring.Net 环境准备和搭建
    第四章 Spring.Net 如何管理您的类___对象的手动装配
    第四章 Spring.Net 如何管理您的类___对象、对象工厂和应用程序上下文
    第四章 Spring.Net 如何管理您的类___对象的自动装配
  • 原文地址:https://www.cnblogs.com/gates/p/3456947.html
Copyright © 2011-2022 走看看