• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#DES加密/解密类库

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Security;
using System.Security.Cryptography;

namespace HyeyClass.Common
{
	#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(key);
			byte[] btIV = Encoding.Default.GetBytes(iv);
			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(key);
			byte[] btIV = Encoding.Default.GetBytes(iv);
			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
}


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
c++对象创建带括号与无括号的区别发布时间:2022-07-14
下一篇:
C#backgroundworker使用方法发布时间:2022-07-14
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap