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 
  • 相关阅读:
    Shell脚本编程(三):shell参数传递
    Java代码里利用Fiddler抓包调试设置
    Shell脚本编程(二):shell变量
    Shell脚本编程(一):初识shell script
    JAVA使用SCANNER接收中文并输出时出现乱码
    RandomAccessFile类理解
    Vue(九):样式绑定v-bind示例
    Dockerfiles ENV和ARG的应用
    dockerfile中设置python虚拟环境+gunicorn启动
    Docker容器 暴露多个端口
  • 原文地址:https://www.cnblogs.com/gates/p/3456947.html
Copyright © 2011-2022 走看看