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 partial class secrettest : System.Web.UI.Page

{
protected string txtFilePath,fileExtName;

Secret jiami = new Secret();

protected void Page_Load(object sender, System.EventArgs e)

{
// 在此处放置用户代码以初始化页面
}

Web Form Designer generated code#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()

{

}
#endregion

protected void btnJia_Click(object sender, System.EventArgs e)

{
txtNew.Text = jiami.DesEncrypt(txtValue.Text,"songxiud");
}

protected void btnJie_Click(object sender, System.EventArgs e)

{
txtValue.Text = jiami.DesDecrypt(txtNew.Text.Trim(),"songxiud");
}

protected 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();
}
}
}

protected 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();
}

protected 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();
}

}
}


<%
@ Page language="c#" Inherits="CommonFunction.secrettest" CodeFile="secret.aspx.cs" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>secret</title>
<meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body bgColor="#ccccff">
<form id="usersecret" method="post" encType="multipart/form-data" runat="server">
<FONT face="宋体">
<asp:textbox id="txtValue" style="Z-INDEX: 101; LEFT: 168px; POSITION: absolute; TOP: 72px" runat="server"></asp:textbox><asp:button id="btnJia" style="Z-INDEX: 102; LEFT: 344px; POSITION: absolute; TOP: 72px" runat="server"
Text="数据加密" onclick="btnJia_Click"></asp:button><asp:button id="btnJie" style="Z-INDEX: 103; LEFT: 344px; POSITION: absolute; TOP: 104px" runat="server"
Text="数据解密" onclick="btnJie_Click"></asp:button><asp:textbox id="txtNew" style="Z-INDEX: 104; LEFT: 168px; POSITION: absolute; TOP: 112px" runat="server"></asp:textbox><asp:textbox id="txtContent" style="Z-INDEX: 105; LEFT: 168px; POSITION: absolute; TOP: 160px"
runat="server" Width="200px" Height="296px" TextMode="MultiLine"></asp:textbox><asp:button id="btnFile" style="Z-INDEX: 106; LEFT: 408px; POSITION: absolute; TOP: 312px" runat="server"
Text="文件加密" onclick="btnFile_Click"></asp:button><asp:button id="btnFileDes" style="Z-INDEX: 107; LEFT: 408px; POSITION: absolute; TOP: 384px"
runat="server" Text="文件解密" onclick="btnFileDes_Click"></asp:button><INPUT id="filePath" style="Z-INDEX: 108; LEFT: 408px; POSITION: absolute; TOP: 160px"
type="file" name="File1" runat="server">
<asp:button id="btnOpen" style="Z-INDEX: 109; LEFT: 408px; POSITION: absolute; TOP: 240px" runat="server"
Text="打开文件" onclick="btnOpen_Click"></asp:button>
<asp:Label id="Label1" style="Z-INDEX: 110; LEFT: 168px; POSITION: absolute; TOP: 32px" runat="server">该示例以txtFile文件夹下的Work.txt文件为例</asp:Label></FONT></form>
</body>
</HTML>

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


{

/**//// <summary>
///数据加密、解密,文件的加密解密
/// </summary>
public class Secret

{
public Secret()

{
//
// TODO: 在此处添加构造函数逻辑
//
}

public string DesEncrypt(string strText, string strEncrKey)

{
byte[] byKey=null;

byte[] IV=
{0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
try

{
byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0,strEncrKey.Length));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write) ;
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());


}
catch(System.Exception error)

{
return "error:" +error.Message+"\r";
}
}

public string DesDecrypt(string strText,string sDecrKey)

{
byte[] byKey = null;

byte[] IV=
{0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
byte[] inputByteArray = new Byte[strText.Length];
try

{
byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0,8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
System.Text.Encoding encoding = new System.Text.UTF8Encoding();
return encoding.GetString(ms.ToArray());
}
catch(System.Exception error)

{
return "error:"+error.Message+"\r";
}

}
public void DesEncrypt(string m_InFilePath,string m_OutFilePath,string strEncrKey)

{
byte[] byKey=null;

byte[] IV=
{0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
try

{
byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0,8));
FileStream fin = new FileStream(m_InFilePath, FileMode.Open, FileAccess.Read);
FileStream fout = new FileStream(m_OutFilePath, FileMode.OpenOrCreate, FileAccess.Write);
fout.SetLength(0);
byte[] bin = new byte[100];
long rdlen = 0;
long totlen = fin.Length;
int len;
DES des = new DESCryptoServiceProvider();
CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
while(rdlen < totlen)

{
len = fin.Read(bin, 0, 100);
encStream.Write(bin, 0, len);
rdlen = rdlen + len;
}

encStream.Close();
fout.Close();
fin.Close();
}
catch

{
}
}

public void DesDecrypt(string m_InFilePath,string m_OutFilePath,string sDecrKey)

{
byte[] byKey = null;

byte[] IV=
{0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
try

{
byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0,8));
FileStream fin = new FileStream(m_InFilePath, FileMode.Open, FileAccess.Read);
FileStream fout = new FileStream(m_OutFilePath, FileMode.OpenOrCreate, FileAccess.Write);
fout.SetLength(0);
byte[] bin = new byte[100];
long rdlen = 0;
long totlen = fin.Length;
int len;

DES des = new DESCryptoServiceProvider();
CryptoStream encStream = new CryptoStream(fout, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);

while(rdlen < totlen)

{
len = fin.Read(bin, 0, 100);
encStream.Write(bin, 0, len);
rdlen = rdlen + len;
}

encStream.Close();
fout.Close();
fin.Close();

}
catch

{
}


}

public string MD5Encrypt(string strText)

{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] result = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(strText));
return System.Text.Encoding.Default.GetString(result);
}

public string MD5Decrypt(string strText)

{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] result = md5.TransformFinalBlock(System.Text.Encoding.Default.GetBytes(strText),0,strText.Length);
return System.Text.Encoding.Default.GetString(result);
}
}
}

posted on
2006-12-28 17:11
Caviare
阅读(
1392)
评论()
收藏
举报