自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#DES加密解密的實(shí)現(xiàn)實(shí)例淺析

開發(fā) 后端
C#DES加密解密的過程是什么呢?那么這里向你詳細(xì)介紹了具體的實(shí)現(xiàn)過程以及方法,希望對你了解和學(xué)習(xí)C#DES加密解密有所幫助。

C# DES加密解密的實(shí)現(xiàn),DES算法為密碼體制中的對稱密碼體制,由IBM公司研制的對稱密碼體制加密算法。其核心為密鑰長度為56位,明文按64位進(jìn)行分組,將分組后的明文組和56位的密鑰按位替代或交換的方法形成密文組的加密方法。

C# DES加密解密的實(shí)現(xiàn)實(shí)例:

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數(shù)組中    
  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加密解密的實(shí)例解析就向你介紹到這里,希望你對C# DES加密解密有所了解,對你應(yīng)用C# DES加密解密有所幫助。

【編輯推薦】

  1. C# MSN Messenger的窗口的實(shí)現(xiàn)淺析
  2. C#MSN插件開發(fā)實(shí)例解析
  3. C#DES算法概念及特點(diǎn)淺析
  4. C#DES算法加密解密實(shí)例解析
  5. C#DES算法實(shí)例解析
責(zé)任編輯:仲衡 來源: steelhome.cn
相關(guān)推薦

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#接口的實(shí)現(xiàn)

2009-08-14 09:50:46

C#復(fù)制構(gòu)造函數(shù)

2009-07-22 11:27:36

iBATIS模糊查詢

2009-08-31 12:31:45

C#創(chuàng)建文件夾

2009-09-04 17:27:46

C# DES

2009-09-03 17:23:45

C#發(fā)送郵件

2009-09-01 16:59:25

C#畫直線

2009-09-01 13:59:01

C#操作Excel

2009-08-17 14:41:47

C#進(jìn)度條實(shí)現(xiàn)

2009-09-09 12:55:59

C# TextBox事

2009-09-03 10:52:41

C#遞歸樹

2009-09-03 17:06:17

C#回車切換焦點(diǎn)

2009-09-02 16:14:21

C#動態(tài)創(chuàng)建數(shù)組

2009-08-27 13:30:11

C# interfac

2009-08-17 17:15:48

C# 進(jìn)度條效果

2009-08-24 10:37:27

C# 泛型

2009-08-31 15:11:23

C#調(diào)用水晶報(bào)表
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號