C#DES加密解密的实现实例浅析

开发 后端
C#DES加密解密的过程是什么呢?那么这里向你详细介绍了具体的实现过程以及方法,希望对你了解和学习C#DES加密解密有所帮助。

C# DES加密解密的实现,DES算法为密码体制中的对称密码体制,由IBM公司研制的对称密码体制加密算法。其核心为密钥长度为56位,明文按64位进行分组,将分组后的明文组和56位的密钥按位替代或交换的方法形成密文组的加密方法。

C# DES加密解密的实现实例:

C# DES加密解密之名称空间  :

  1. using  System;    
  2. using  System.Security.Cryptography;    
  3. using  System.IO;    
  4. using  System.Text;   

C# DES加密解密之方法 : 

  1. //加密方法    
  2. publicstring  Encrypt(string  pToEncrypt,  string  sKey)    
  3. {    
  4.  DESCryptoServiceProvider  des  =  new  DESCryptoServiceProvider();    
  5.  //把字符串放到byte数组中    
  6.   //原来使用的UTF8编码,我改成Unicode编码了,不行    
  7.  byte[]  inputByteArray  =  Encoding.Default.GetBytes(pToEncrypt);    
  8.  //byte[]  inputByteArray=Encoding.Unicode.GetBytes(pToEncrypt);  

C# DES加密解密之建立加密对象的密钥和偏移量 

  1.  //原文使用ASCIIEncoding.ASCII方法的GetBytes方法    
  2.  //使得输入密码必须输入英文文本    
  3.  des.Key  =  ASCIIEncoding.ASCII.GetBytes(sKey);    
  4.  des.IV  =  ASCIIEncoding.ASCII.GetBytes(sKey);    
  5.  MemoryStream  ms  =  new  MemoryStream();    
  6.  CryptoStream  cs  =  new  CryptoStream(  
  7. ms,  des.CreateEncryptor(),CryptoStreamMode.Write);    
  8.  //Write  the  byte  array  into  the  crypto  stream    
  9.  //(It  will  end  up  in  the  memory  stream)    
  10.  cs.Write(inputByteArray,  0,  inputByteArray.Length);    
  11.  cs.FlushFinalBlock();    
  12.  //Get  the  data  back  from  the  memory  stream,  and  into  a  string    
  13.  StringBuilder  ret  =  new  StringBuilder();    
  14.  foreach(byte  b  in  ms.ToArray())    
  15.    {    
  16.    //Format  as  hex    
  17.    ret.AppendFormat("{0:X2}",  b);    
  18.    }    
  19.  ret.ToString();    
  20.  return  ret.ToString();    
  21. }   

C# DES加密解密之解密方法 

  1. publicstring  Decrypt(string  pToDecrypt,  string  sKey)    
  2. {    
  3.  DESCryptoServiceProvider  des  =  new  DESCryptoServiceProvider();    
  4.  
  5.  //Put  the  input  string  into  the  byte  array    
  6.  byte[]  inputByteArray  =  new  byte[pToDecrypt.Length  /  2];    
  7.  for(int  x  =  0;  x  <  pToDecrypt.Length  /  2;  x++)    
  8.  {    
  9.  int  i  =  (Convert.ToInt32(pToDecrypt.Substring(x  *  2,  2),  16));    
  10. inputByteArray[x]  =  (byte)i;    
  11.  }   

 

C# DES加密解密之建立加密对象的密钥和偏移量,此值重要,不能修改 

  1.  des.Key  =  ASCIIEncoding.ASCII.GetBytes(sKey);    
  2.  des.IV  =  ASCIIEncoding.ASCII.GetBytes(sKey);    
  3.  MemoryStream  ms  =  new  MemoryStream();    
  4.  CryptoStream  cs  =  new  CryptoStream(ms,    
  5. des.CreateDecryptor(),CryptoStreamMode.Write);    
  6.  //Flush  the  data  through  the  crypto  stream  into  the  memory  stream    
  7.  cs.Write(inputByteArray,  0,  inputByteArray.Length);    
  8.  cs.FlushFinalBlock();    
  9.  
  10.  //Get  the  decrypted  data  back  from  the  memory  stream    
  11.  //建立StringBuild对象,  
  12. //CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象    
  13.  StringBuilder  ret  =  new  StringBuilder();    
  14.      
  15.  return  System.Text.Encoding.Default.GetString(ms.ToArray());    
  16. }  

C# DES加密解密的实例解析就向你介绍到这里,希望你对C# DES加密解密有所了解,对你应用C# DES加密解密有所帮助。

【编辑推荐】

  1. C# MSN Messenger的窗口的实现浅析
  2. C#MSN插件开发实例解析
  3. C#DES算法概念及特点浅析
  4. C#DES算法加密解密实例解析
  5. C#DES算法实例解析
责任编辑:仲衡 来源: steelhome.cn
相关推荐

2009-09-04 16:45:44

C# DES算法加密解

2009-09-04 16:55:09

C#DES算法解密

2009-09-04 16:37:37

C# DES算法

2009-08-27 18:09:49

C#接口的实现

2009-08-14 09:50:46

C#复制构造函数

2009-07-22 11:27:36

iBATIS模糊查询

2009-08-31 12:31:45

C#创建文件夹

2009-09-04 17:27:46

C# DES

2009-09-03 17:23:45

C#发送邮件

2009-09-01 16:59:25

C#画直线

2009-09-01 13:59:01

C#操作Excel

2009-08-17 14:41:47

C#进度条实现

2009-09-09 12:55:59

C# TextBox事

2009-09-03 10:52:41

C#递归树

2009-09-03 17:06:17

C#回车切换焦点

2009-09-02 16:14:21

C#动态创建数组

2009-08-17 17:15:48

C# 进度条效果

2009-08-24 10:37:27

C# 泛型

2009-08-27 13:30:11

C# interfac

2019-08-02 10:43:57

点赞
收藏

51CTO技术栈公众号