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

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

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

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

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

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

C# DES加密解密之方法 : 

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

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

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

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

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

 

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

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

C#进度条实现

2009-09-01 13:59:01

C#操作Excel

2009-09-09 12:55:59

C# TextBox事

2009-09-03 10:52:41

C#递归树

2009-09-02 16:14:21

C#动态创建数组

2009-09-03 17:06:17

C#回车切换焦点

2009-08-17 17:15:48

C# 进度条效果

2009-08-24 10:37:27

C# 泛型

2009-08-27 13:30:11

C# interfac

2009-09-03 12:52:50

C#打开记事本
点赞
收藏

51CTO技术栈公众号