C#MD5加密解密


using System.Security.Cryptography;
 using  System.IO; 
 using  System.Text;

///MD5加密
  public string MD5Encrypt(string  pToEncrypt,  string  sKey)
  { 
   DESCryptoServiceProvider  des  =  new  DESCryptoServiceProvider(); 
   byte[]  inputByteArray  =  Encoding.Default.GetBytes(pToEncrypt); 
   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); 
   cs.Write(inputByteArray,  0,  inputByteArray.Length); 
   cs.FlushFinalBlock(); 
   StringBuilder  ret  =  new  StringBuilder(); 
   foreach(byte  b  in  ms.ToArray()) 
   { 
    ret.AppendFormat("{0:X2}",  b); 
   } 
   ret.ToString(); 
   return  ret.ToString(); 


  }

  ///MD5解密
  public string MD5Decrypt(string  pToDecrypt,  string  sKey)
  {
   DESCryptoServiceProvider  des  =  new  DESCryptoServiceProvider(); 
 
   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; 
   } 
 
   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); 
   cs.Write(inputByteArray,  0,  inputByteArray.Length); 
   cs.FlushFinalBlock(); 
 
   StringBuilder  ret  =  new  StringBuilder(); 
            
   return  System.Text.Encoding.Default.GetString(ms.ToArray()); 
  }

  


上一篇: CCTV历届春晚视频回放
下一篇: ASP.NET中使用web.config配置web应用程序中的数据库连接
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
相关日志:
评论: 1 | 引用: 0 | 查看次数: -
Erebus[2011-07-03 02:42 PM | | | 219.143.205.111 | del | 回复回复]
沙发
实在忍不住评论你文章,劝楼主把什么是MD5搞清楚再写博客吧。MD5是哈希函数,哪来的解密?你函数中做的是DES加密和解密,和MD5有半毛钱关系。
发表评论
昵 称:
密 码: 游客发言不需要密码.
邮 箱: 邮件地址支持Gravatar头像,邮箱地址不会公开.
网 址: 输入网址便于回访.
内 容:
验证码:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 200 字 | UBB代码 关闭 | [img]标签 关闭