• 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         }
  • 相关阅读:
    linux下查看主板内存槽与内存信息
    centos 6 与 centos 7 服务开机启动、关闭设置的方法
    linux 配置本地光盘YUM源
    linux crontab定时任务不执行
    Linux下安装MySQL5.6
    Linux shell 自动删除n天前日志
    nginx request_time 和upstream_response_time
    linux中文件多行合并为一行的例子
    awk 处理文本:行转列,列转行
    nfs环境搭建报错clnt_create: RPC: Program not registered
  • 原文地址:https://www.cnblogs.com/ink-heart/p/5898761.html
Copyright © 2020-2023  润新知