C# DES加密解密算法

C# DES加密解密算法

#region DES Class /// <summary> /// ClassName: DES 加密类 /// DES加密、解密类库,字符串加密结果使用BASE64编码返回,支持文件的加密和解密 /// Time:2010-04-30 /// Auther:Hyey.wl /// DES 的摘要说明。 /// </summary> public sealed class DES { /// <summary> /// myiv is iv /// </summary> string myiv = "Hyey20100430"; /// <summary> /// mykey is key /// </summary> string mykey = "HyeyWl30"; /// <summary> /// DES加密偏移量 /// 必须是>=8位长的字符串 /// </summary> public string IV { get { return myiv; } set { myiv = value; } } /// <summary> /// DES加密的私钥 /// 必须是8位长的字符串 /// </summary> public string Key { get { return mykey; } set { mykey = value; } } public DES() { // // TODO: 在此处添加构造函数逻辑 // } /// <summary> /// 对字符串进行DES加密 /// Encrypts the specified sourcestring. /// </summary> /// <param name="sourcestring">The sourcestring.待加密的字符串</param> /// <returns>加密后的BASE64编码的字符串</returns> public string Encrypt(string sourceString) { byte[] btKey = Encoding.Default.GetBytes(mykey); byte[] btIV = Encoding.Default.GetBytes(myiv); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); using (MemoryStream ms = new MemoryStream()) { byte[] inData = Encoding.Default.GetBytes(sourceString); try { using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write)) { cs.Write(inData, 0, inData.Length); cs.FlushFinalBlock(); } return Convert.ToBase64String(ms.ToArray()); } catch { throw; } } } /// <summary> /// Decrypts the specified encrypted string. /// 对DES加密后的字符串进行解密 /// </summary> /// <param name="encryptedString">The encrypted string.待解密的字符串</param> /// <returns>解密后的字符串</returns> public string Decrypt(string encryptedString) { byte[] btKey = Encoding.Default.GetBytes(mykey); byte[] btIV = Encoding.Default.GetBytes(myiv); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); using (MemoryStream ms = new MemoryStream()) { byte[] inData = Convert.FromBase64String(encryptedString); try { using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write)) { cs.Write(inData, 0, inData.Length); cs.FlushFinalBlock(); } return Encoding.Default.GetString(ms.ToArray()); } catch { throw; } } } /// <summary> /// Encrypts the file. /// 对文件内容进行DES加密 /// </summary> /// <param name="sourceFile">The source file.待加密的文件绝对路径</param> /// <param name="destFile">The dest file.加密后的文件保存的绝对路径</param> public void EncryptFile(string sourceFile, string destFile) { if (!File.Exists(sourceFile)) throw new FileNotFoundException("指定的文件路径不存在!", sourceFile); byte[] btKey = Encoding.Default.GetBytes(mykey); byte[] btIV = Encoding.Default.GetBytes(myiv); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] btFile = File.ReadAllBytes(sourceFile); using (FileStream fs = new FileStream(destFile, FileMode.Create, FileAccess.Write)) { try { using (CryptoStream cs = new CryptoStream(fs, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write)) { cs.Write(btFile, 0, btFile.Length); cs.FlushFinalBlock(); } } catch { throw; } finally { fs.Close(); } } } /// <summary> /// Encrypts the file. /// 对文件内容进行DES加密,加密后覆盖掉原来的文件 /// </summary> /// <param name="sourceFile">The source file.待加密的文件的绝对路径</param> public void EncryptFile(string sourceFile) { EncryptFile(sourceFile, sourceFile); } /// <summary> /// Decrypts the file. /// 对文件内容进行DES解密 /// </summary> /// <param name="sourceFile">The source file.待解密的文件绝对路径</param> /// <param name="destFile">The dest file.解密后的文件保存的绝对路径</param> public void DecryptFile(string sourceFile, string destFile) { if (!File.Exists(sourceFile)) throw new FileNotFoundException("指定的文件路径不存在!", sourceFile); byte[] btKey = Encoding.Default.GetBytes(mykey); byte[] btIV = Encoding.Default.GetBytes(myiv); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] btFile = File.ReadAllBytes(sourceFile); using (FileStream fs = new FileStream(destFile, FileMode.Create, FileAccess.Write)) { try { using (CryptoStream cs = new CryptoStream(fs, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write)) { cs.Write(btFile, 0, btFile.Length); cs.FlushFinalBlock(); } } catch { throw; } finally { fs.Close(); } } } /// <summary> /// Decrypts the file. /// 对文件内容进行DES解密,加密后覆盖掉原来的文件. /// </summary> /// <param name="sourceFile">The source file.待解密的文件的绝对路径.</param> public void DecryptFile(string sourceFile) { DecryptFile(sourceFile, sourceFile); } } #endregion

源:http://www.cnblogs.com/Leo_wl/archive/2010/04/30/1725168.html

转载于:https://www.cnblogs.com/jiang1984j/archive/2010/08/09/1986748.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值