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


本文摘自PHP中文网,作者怪我咯,侵删。

//加密

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

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";

  }

 }

//解密

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

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加密解密的代码的详细内容!

相关阅读 >>

web api的 asp.net属性路由实例详解

asp.net中有关config文件的读写功能讲解

asp.net mvc实现404跳转的代码实例

分享asp.net学习笔记(6)webpages 表单

asp.net之exceptionfilter过滤器

关于操作 asp.net web api的实例

asp.net core razor页面路由的详细介绍

asp.net mvc如何动态编译生成controller的方法示例详解

使用asp.net中mvc引擎开发插件系统的示例详解

分享19个asp脚本语言的基本技巧

更多相关阅读请进入《asp.net》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...