zoukankan      html  css  js  c++  java
  • DES加密 DES解密

     1         public static string _KEY = "HQDCKEY1";  //密钥  
     2         public static string _IV = "HQDCKEY2";   //向量  
     3 
     4         /// <summary>  
     5         /// DES加密  
     6         /// </summary>  
     7         /// <param name="data"></param>  
     8         /// <returns></returns>  
     9         public static string Encode(string data)
    10         {
    11 
    12             byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(_KEY);
    13             byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(_IV);
    14 
    15             DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
    16             int i = cryptoProvider.KeySize;
    17             MemoryStream ms = new MemoryStream();
    18             CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);
    19 
    20             StreamWriter sw = new StreamWriter(cst);
    21             sw.Write(data);
    22             sw.Flush();
    23             cst.FlushFinalBlock();
    24             sw.Flush();
    25 
    26             string strRet = Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
    27             return strRet;
    28         }
    29 
    30         /// <summary>  
    31         /// DES解密  
    32         /// </summary>  
    33         /// <param name="data"></param>  
    34         /// <returns></returns>  
    35         public static string Decode(string data)
    36         {
    37 
    38             byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(_KEY);
    39             byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(_IV);
    40 
    41             byte[] byEnc;
    42 
    43             try
    44             {
    45                 data.Replace("_%_", "/");
    46                 data.Replace("-%-", "#");
    47                 byEnc = Convert.FromBase64String(data);
    48 
    49             }
    50             catch
    51             {
    52                 return null;
    53             }
    54 
    55             DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
    56             MemoryStream ms = new MemoryStream(byEnc);
    57             CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
    58             StreamReader sr = new StreamReader(cst);
    59             return sr.ReadToEnd();
    60         }
  • 相关阅读:
    initializer_list形参
    前置递增运算符和后置递增运算符的区别
    基本的TCP socket API
    C++ find()函数
    python-对目录下的文件按时间排序
    js常用方法
    selenium中的对文本进行全选,复制,粘贴,剪切和删除的操作
    python 打包exe 命令及去除 cmd框
    mysql命令行修改密码
    html文件转换成excel
  • 原文地址:https://www.cnblogs.com/ink-heart/p/5898761.html
Copyright © 2011-2022 走看看