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

C# DES加密解密的完成,DES算法为暗码体系中的对称暗码体系,由IBM公司研发的对称暗码体系加密算法。其中心为密钥长度为56位,明文按64位进行分组,将分组后的明文组和56位的密钥按位代替或交流的办法构成密文组的加密办法。

C#DES加密解密的完成实例浅析(c++实现des加密解密)  C# DES加密解密 第1张

C# DES加密解密的完成实例:

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

  1. usingSystem;
  2. usingSystem.Security.Cryptography;
  3. usingSystem.IO;
  4. usingSystem.Text;

C# DES加密解密之办法 :

  1. //加密办法
  2. publicstringEncrypt(stringpToEncrypt,stringsKey)
  3. {
  4. DESCryptoServiceProviderdes=newDESCryptoServiceProvider();
  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. MemoryStreamms=newMemoryStream();
  6. CryptoStreamcs=newCryptoStream(
  7. ms,des.CreateEncryptor(),CryptoStreamMode.Write);
  8. //Writethebytearrayintothecryptostream
  9. //(Itwillendupinthememorystream)
  10. cs.Write(inputByteArray,0,inputByteArray.Length);
  11. cs.FlushFinalBlock();
  12. //Getthedatabackfromthememorystream,andintoastring
  13. StringBuilderret=newStringBuilder();
  14. foreach(bytebinms.ToArray())
  15. {
  16. //Formatashex
  17. ret.AppendFormat("{0:X2}",b);
  18. }
  19. ret.ToString();
  20. returnret.ToString();
  21. }

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

  1. publicstringDecrypt(stringpToDecrypt,stringsKey)
  2. {
  3. DESCryptoServiceProviderdes=newDESCryptoServiceProvider();
  4. //Puttheinputstringintothebytearray
  5. byte[]inputByteArray=newbyte[pToDecrypt.Length/2];
  6. for(intx=0;x<pToDecrypt.Length/2;x++)
  7. {
  8. inti=(Convert.ToInt32(pToDecrypt.Substring(x*2,2),16));
  9. inputByteArray[x]=(byte)i;
  10. }

C# DES加密解密之树立加密目标的密钥和偏移量,此值重要,不能修正

  1. des.Key=ASCIIEncoding.ASCII.GetBytes(sKey);
  2. des.IV=ASCIIEncoding.ASCII.GetBytes(sKey);
  3. MemoryStreamms=newMemoryStream();
  4. CryptoStreamcs=newCryptoStream(ms,
  5. des.CreateDecryptor(),CryptoStreamMode.Write);
  6. //Flushthedatathroughthecryptostreamintothememorystream
  7. cs.Write(inputByteArray,0,inputByteArray.Length);
  8. cs.FlushFinalBlock();
  9. //Getthedecrypteddatabackfromthememorystream
  10. //树立StringBuild目标,
  11. //CreateDecrypt运用的是流目标,有必要把解密后的文本变成流目标
  12. StringBuilderret=newStringBuilder();
  13. returnSystem.Text.Encoding.Default.GetString(ms.ToArray());
  14. }

C# DES加密解密的实例解析就向你介绍到这儿,期望你对C# DES加密解密有所了解,对你使用C# DES加密解密有所协助。

【修改引荐】

  1. C# MSN Messenger的窗口的完成浅析
  2. C#MSN插件开发实例解析
  3. C#DES算法概念及特色浅析
  4. C#DES算法加密解密实例解析
  5. C#DES算法实例解析
转载请说明出处
知优网 » C#DES加密解密的完成实例浅析(c++实现des加密解密)

发表评论

您需要后才能发表评论