用C#实现DES加密解密

MD5/Base64/DES是常用来对用户身份验证的算法,本人陆续公开了自己常用的这三种算法的源代码,不足之处,希望大家批评指正。
 
InBlock.gif using System; 
InBlock.gif using System.Security.Cryptography; 
InBlock.gif using System.Text; 
InBlock.gif using System.IO; 
InBlock.gif 
InBlock.gif namespace Common 
InBlock.gif...{ 
InBlock.gif        /** <summary> 
InBlock.gif         /// DESEncrypt加密解密算法。 
InBlock.gif         /// </summary> 
InBlock.gif         public  sealed  class DESEncrypt 
InBlock.gif        ...{ 
InBlock.gif                 private DESEncrypt() 
InBlock.gif                ...{ 
InBlock.gif                         // 
InBlock.gif                         // TODO: 在此处添加构造函数逻辑 
InBlock.gif                         // 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 private  static  string key =  "zhoufoxcn"
InBlock.gif 
InBlock.gif                /** <summary> 
InBlock.gif                 /// 对称加密解密的密钥 
InBlock.gif                 /// </summary> 
InBlock.gif                 public  static  string Key 
InBlock.gif                ...{ 
InBlock.gif                        get 
InBlock.gif                        ...{ 
InBlock.gif                                 return key; 
InBlock.gif                        } 
InBlock.gif                        set 
InBlock.gif                        ...{ 
InBlock.gif                                key = value; 
InBlock.gif                        } 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                /** <summary> 
InBlock.gif                 /// DES加密 
InBlock.gif                 /// </summary> 
InBlock.gif                 /// <param name="encryptString"></param> 
InBlock.gif                 /// <returns></returns> 
InBlock.gif                 public  static  string DesEncrypt( string encryptString) 
InBlock.gif                ...{ 
InBlock.gif                         byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8)); 
InBlock.gif                         byte[] keyIV = keyBytes; 
InBlock.gif                         byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString); 
InBlock.gif                        DESCryptoServiceProvider provider =  new DESCryptoServiceProvider(); 
InBlock.gif                        MemoryStream mStream =  new MemoryStream(); 
InBlock.gif                        CryptoStream cStream =  new CryptoStream(mStream, provider.CreateEncryptor(keyBytes, keyIV), CryptoStreamMode.Write); 
InBlock.gif                        cStream.Write(inputByteArray, 0, inputByteArray.Length); 
InBlock.gif                        cStream.FlushFinalBlock(); 
InBlock.gif                         return Convert.ToBase64String(mStream.ToArray()); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                /** <summary> 
InBlock.gif                 /// DES解密 
InBlock.gif                 /// </summary> 
InBlock.gif                 /// <param name="decryptString"></param> 
InBlock.gif                 /// <returns></returns> 
InBlock.gif                 public  static  string DesDecrypt( string decryptString) 
InBlock.gif                ...{ 
InBlock.gif                         byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8)); 
InBlock.gif                         byte[] keyIV = keyBytes; 
InBlock.gif                         byte[] inputByteArray = Convert.FromBase64String(decryptString); 
InBlock.gif                        DESCryptoServiceProvider provider =  new DESCryptoServiceProvider(); 
InBlock.gif                        MemoryStream mStream =  new MemoryStream(); 
InBlock.gif                        CryptoStream cStream =  new CryptoStream(mStream, provider.CreateDecryptor(keyBytes, keyIV), CryptoStreamMode.Write); 
InBlock.gif                        cStream.Write(inputByteArray, 0, inputByteArray.Length); 
InBlock.gif                        cStream.FlushFinalBlock(); 
InBlock.gif                         return Encoding.UTF8.GetString(mStream.ToArray()); 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif














本文转自周金桥51CTO博客,原文链接:http://blog.51cto.com/zhoufoxcn/167095,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值