• 技术文章 >后端开发 >C#.Net教程

    一段asp.net DES加密解密的代码

    怪我咯怪我咯2017-03-30 11:37:43原创1165

    //加密

     public string DesEncrypt(string strText, string strEncrKey) 
      { 
       byte[] byKey=null; 
       byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF}; 
       try 
       { 
        byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0,8)); 
        DESCryptoServiceProvider des = new DESCryptoServiceProvider(); 
        byte[] inputByteArray =System.Text.Encoding.UTF8.GetBytes(strText); 
        MemoryStream ms = new MemoryStream(); 
        CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write) ; 
        cs.Write(inputByteArray, 0, inputByteArray.Length); 
        cs.FlushFinalBlock(); 
        return Convert.ToBase64String(ms.ToArray()); 
       } 
       catch(System.Exception error) 
       { 
        MessageBox.Show(error.Message); 
        return "error:" +error.Message+"/r"; 
       } 
      }

    //解密

     public string DesDecrypt(string strText,string sDecrKey) 
      { 
       byte[] byKey = null; 
       byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF}; 
       byte[] inputByteArray = new Byte[strText.Length]; 
       try 
       { 
        byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0,8)); 
        DESCryptoServiceProvider des = new DESCryptoServiceProvider(); 
        inputByteArray = Convert.FromBase64String(strText); 
        MemoryStream ms = new MemoryStream(); 
        CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write); 
        cs.Write(inputByteArray, 0, inputByteArray.Length); 
        cs.FlushFinalBlock(); 
        System.Text.Encoding encoding = new System.Text.UTF8Encoding(); 
        return encoding.GetString(ms.ToArray()); 
       } 
       catch(System.Exception error) 
       { 
        MessageBox.Show(error.Message); 
        return "error:"+error.Message+"/r"; 
       } 
      }


    以上就是一段asp.net DES加密解密的代码的详细内容,更多请关注php中文网其它相关文章!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:asp.net,DES
    上一篇:C#中常用的正则表达式总结分享 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • ASP.NET使用Ajax如何返回Json对象的方法具体介绍• 应用绝对路径与相对路径• 解决asp.net中“从客户端中检测到有潜在危险的Request.Form值”的错误• 详解ASP.NET配置文件Web.config• asp.net 图片验证码的HtmlHelper
    1/1

    PHP中文网