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
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// secret 的摘要说明。
/// </summary>
public partial class secrettest : System.Web.UI.Page
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
protected string txtFilePath,fileExtName;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Secret jiami = new Secret();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected void Page_Load(object sender, System.EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// 在此处放置用户代码以初始化页面
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Web Form Designer generated code#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected void btnJia_Click(object sender, System.EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
txtNew.Text = jiami.DesEncrypt(txtValue.Text,"songxiud");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected void btnJie_Click(object sender, System.EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
txtValue.Text = jiami.DesDecrypt(txtNew.Text.Trim(),"songxiud");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected void btnOpen_Click(object sender, System.EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(filePath.PostedFile.FileName != "")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
txtFilePath =filePath.PostedFile.FileName;
fileExtName = txtFilePath.Substring(txtFilePath.LastIndexOf(".")+1,3);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if(fileExtName !="txt" && fileExtName != "TXT")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Response.Write("请选择文本文件");
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
StreamReader fileStream = new StreamReader(txtFilePath,Encoding.Default);
txtContent.Text = fileStream.ReadToEnd();
fileStream.Close();
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected void btnFile_Click(object sender, System.EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected void btnFileDes_Click(object sender, System.EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
@ 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>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using System;
using System.Security.Cryptography;
using System.IO;
using System.Text;
namespace CommonFunction
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
///数据加密、解密,文件的加密解密
/// </summary>
public class Secret
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public Secret()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
// TODO: 在此处添加构造函数逻辑
//
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string DesEncrypt(string strText, string strEncrKey)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
byte[] byKey=null;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
byte[] IV=
{0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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());
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
catch(System.Exception error)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return "error:" +error.Message+"\r";
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string DesDecrypt(string strText,string sDecrKey)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
byte[] byKey = null;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
byte[] IV=
{0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
byte[] inputByteArray = new Byte[strText.Length];
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return "error:"+error.Message+"\r";
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
public void DesEncrypt(string m_InFilePath,string m_OutFilePath,string strEncrKey)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
byte[] byKey=null;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
byte[] IV=
{0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
len = fin.Read(bin, 0, 100);
encStream.Write(bin, 0, len);
rdlen = rdlen + len;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
encStream.Close();
fout.Close();
fin.Close();
}
catch
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void DesDecrypt(string m_InFilePath,string m_OutFilePath,string sDecrKey)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
byte[] byKey = null;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
byte[] IV=
{0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
DES des = new DESCryptoServiceProvider();
CryptoStream encStream = new CryptoStream(fout, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
while(rdlen < totlen)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
len = fin.Read(bin, 0, 100);
encStream.Write(bin, 0, len);
rdlen = rdlen + len;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
encStream.Close();
fout.Close();
fin.Close();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
catch
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string MD5Encrypt(string strText)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] result = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(strText));
return System.Text.Encoding.Default.GetString(result);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string MD5Decrypt(string strText)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] result = md5.TransformFinalBlock(System.Text.Encoding.Default.GetBytes(strText),0,strText.Length);
return System.Text.Encoding.Default.GetString(result);
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
posted on
2006-12-28 17:11
Caviare
阅读(
1380)
评论()
编辑
收藏
举报