using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Security.Cryptography;
using System.IO;
using System.Text;
namespace CommonFunction
{
	/// <summary>
	/// secret 的摘要说明。
	/// </summary>
	public class secrettest : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.TextBox txtValue;
		protected System.Web.UI.WebControls.Button btnJia;
		protected System.Web.UI.WebControls.Button btnJie;
		protected System.Web.UI.WebControls.TextBox txtNew;
		protected System.Web.UI.WebControls.TextBox txtContent;
		protected System.Web.UI.WebControls.Button btnFile;
		protected System.Web.UI.WebControls.Button btnFileDes;
		protected System.Web.UI.WebControls.Button btnOpen;
		protected System.Web.UI.HtmlControls.HtmlInputFile filePath;
		protected string txtFilePath,fileExtName;
		protected System.Web.UI.WebControls.Label Label1;

		Secret jiami = new Secret();

		private void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面
		}
		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN：该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    
			this.btnJia.Click += new System.EventHandler(this.btnJia_Click);
			this.btnJie.Click += new System.EventHandler(this.btnJie_Click);
			this.btnFile.Click += new System.EventHandler(this.btnFile_Click);
			this.btnFileDes.Click += new System.EventHandler(this.btnFileDes_Click);
			this.btnOpen.Click += new System.EventHandler(this.btnOpen_Click);

		}
		#endregion

		private void btnJia_Click(object sender, System.EventArgs e)
		{
			txtNew.Text = jiami.DesEncrypt(txtValue.Text,"songxiud");
		}

		private void btnJie_Click(object sender, System.EventArgs e)
		{
			txtValue.Text = jiami.DesDecrypt(txtNew.Text.Trim(),"songxiud");
		}

		private void btnOpen_Click(object sender, System.EventArgs e)
		{
			if(filePath.PostedFile.FileName != "")
			{
				txtFilePath =filePath.PostedFile.FileName;
				fileExtName = txtFilePath.Substring(txtFilePath.LastIndexOf(".")+1,3);

				if(fileExtName !="txt" && fileExtName != "TXT")
				{
					Response.Write("请选择文本文件");
				}
				else
				{
					StreamReader fileStream = new StreamReader(txtFilePath,Encoding.Default);
					txtContent.Text = fileStream.ReadToEnd();
					fileStream.Close();
				}
			}
		}

		private void btnFile_Click(object sender, System.EventArgs e)
		{
			jiami.DesEncrypt(Server.MapPath("txtFile")+"\\work.txt",Server.MapPath("txtFile")+"\\work1.txt","songxiud");
			StreamReader fileStream = new StreamReader(Server.MapPath("txtFile")+"\\work1.txt",Encoding.Default);
			txtContent.Text = fileStream.ReadToEnd();
			fileStream.Close();
		}

		private void btnFileDes_Click(object sender, System.EventArgs e)
		{
			jiami.DesDecrypt(Server.MapPath("txtFile")+"\\work1.txt",Server.MapPath("txtFile")+"\\workjie.txt","songxiud");
			StreamReader fileStream = new StreamReader(Server.MapPath("txtFile")+"\\workjie.txt",Encoding.Default);
			txtContent.Text = fileStream.ReadToEnd();
			fileStream.Close();
		}

	
	}
}
