在最近的一个.net项目中,需要使用到加密技术。就是根据:用户提供的一个key,来加密一些字符信息,最后可以根据这个key来解密信息!小弟当时确实为此,急的直抓头。在google,百度中翻来覆去的找资料。但是,确实很失望,没有一个很好的例子。百般无奈,自己查看MSDN,终于,写出了下面这个类!用它可以实现前面用户提出的需求。现在拿出来和大家分享!
在这个类中主要应用到了.net的两个基类(要不然很真不知道怎么办),一个是System.Security,另一个是System.Security.Cryptography,很明显一看就知道是Microsoft用来做加密信息的。废话不多说,让我们来看这个类!
加密,解密功能函数#region 加密,解密功能函数
using System;
using System.IO;
using System.Text;
using System.Security;
using System.Security.Cryptography;
namespace Utility.Common
{
/**//**//**//// <summary>
/// 加密,解密功能函数
/// </summary>
public class EncryptionUtil
{
public EncryptionUtil()
{
//
// TODO: Add constructor logic here
//
}
static private Byte[] m_Key = new Byte[8];
static private Byte[] m_IV = new Byte[8];
//为了安全,直接将key写死在文件中,你也可以用一个属性来实现
public string key = "test$)";
/**//**//**///////////////////////////
//加密函数
public string EncryptData(String strKey, String strData)
{
string strResult; //Return Result
//1. 字符大小不能超过 90Kb. 否则, 缓存容易溢出(看第3点)
if (strData.Length > 92160)
{
strResult="Error. Data String too large. Keep within 90Kb.";
return strResult;
}
//2. 生成key
if (!InitKey(strKey))
{
strResult="Error. Fail to generate key for encryption";
return strResult;
}
//3. 准备处理的字符串
//字符串的前5个字节用来存储数据的长度
//用这个简单的方法来记住数据的初始大小,没有用太复杂的方法
strData = String.Format("{0,5:00000}"+strData, strData.Length);
//4. 加密数据
byte[] rbData = new byte[strData.Length];
ASCIIEncoding aEnc = new ASCIIEncoding();
aEnc.GetBytes(strData, 0, strData.Length, rbData, 0);
//加密功能实现的主要类
DESCryptoServiceProvider descsp = new DESCryptoServiceProvider();
ICryptoTransform desEncrypt = descsp.CreateEncryptor(m_Key, m_IV);
//5. 准备stream
// mOut是输出流.
// mStream是输入流
// cs为转换流
MemoryStream mStream = new MemoryStream(rbData);
CryptoStream cs = new CryptoStream(mStream, desEncrypt, CryptoStreamMode.Read);
MemoryStream mOut = new MemoryStream();
//6. 开始加密
int bytesRead;
byte[] output = new byte[1024];
do
{
bytesRead = cs.Read(output,0,1024);
if (bytesRead != 0)
mOut.Write(output,0,bytesRead);
} while (bytesRead > 0);
//7. 返回加密结果
//因为是一个web项目,在这里转换为base64,因此在http上是不会出错的
if (mOut.Length == 0)
strResult = "";
else
strResult = Convert.ToBase64String(mOut.GetBuffer(), 0, (int)mOut.Length);
return strResult;
}
/**//**//**///////////////////////////
//解密函数
public string DecryptData(String strKey, String strData)
{
string strResult;
//1. 生成解密key
if (!InitKey(strKey))
{
strResult="Error. Fail to generate key for decryption";
return strResult;
}
//2. 初始化解密的主要类
int nReturn = 0;
DESCryptoServiceProvider descsp = new DESCryptoServiceProvider();
ICryptoTransform desDecrypt = descsp.CreateDecryptor(m_Key, m_IV);
//3. 准备stream
// mOut为输出流
// cs为转换流
MemoryStream mOut = new MemoryStream();
CryptoStream cs = new CryptoStream(mOut, desDecrypt, CryptoStreamMode.Write);
byte[] bPlain = new byte[strData.Length];
try
{
bPlain=Convert.FromBase64CharArray(strData.ToCharArray(),0,strData.Length);
}
catch (Exception)
{
strResult = "Error. Input Data is not base64 encoded.";
return strResult;
}
long lRead = 0;
long lTotal = strData.Length;
try
{
//5. 完成解密
while (lTotal >= lRead)
{
cs.Write(bPlain,0,(int)bPlain.Length);
lRead = mOut.Length + Convert.ToUInt32(((bPlain.Length / descsp.BlockSize) * descsp.BlockSize));
};
ASCIIEncoding aEnc = new ASCIIEncoding();
strResult = aEnc.GetString(mOut.GetBuffer(), 0, (int)mOut.Length);
//6.去处存储长度的前5个字节的数据
String strLen = strResult.Substring(0,5);
int nLen = Convert.ToInt32(strLen);
strResult = strResult.Substring(5, nLen);
nReturn = (int)mOut.Length;
return strResult;
}
catch (Exception)
{
strResult = "Error. Decryption Failed. Possibly due to incorrect Key or corrputed data";
return strResult;
}
}
/**//**//**//////////////////////////////////////////////////////////////
//生成key的函数
static private bool InitKey(String strKey)
{
try
{
// 转换key为字节流
byte[] bp = new byte[strKey.Length];
ASCIIEncoding aEnc = new ASCIIEncoding();
aEnc.GetBytes(strKey, 0, strKey.Length, bp, 0);
SHA1CryptoServiceProvider sha = new SHA1CryptoServiceProvider();
byte[] bpHash = sha.ComputeHash(bp);
int i;
// 生成初始化DESCryptoServiceProvider的参数
for (i=0; i<8; i++)
m_Key[i] = bpHash[i];
for (i=8; i<16; i++)
m_IV[i-8] = bpHash[i];
return true;
}
catch (Exception)
{
//错误处理
return false;
}
}
}
}
#endregion 好的,我们下面来看如何使用这个类加密,解密字符数据前台页面:
<%@ Page language="c#" Codebehind="Encode.aspx.cs" AutoEventWireup="false" Inherits="WebUI.Encode" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Encode</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<TABLE id="Table1" style="Z-INDEX: 107; LEFT: 40px; WIDTH: 688px; POSITION: absolute; TOP: 32px; HEIGHT: 118px"
cellSpacing="0" cellPadding="1" width="688" border="1">
<TR>
<TD style="WIDTH: 111px">
<asp:Label id="Label1" runat="server">Clear String</asp:Label></TD>
<TD>
<asp:TextBox id="txt_ClearString" runat="server" Width="440px"></asp:TextBox></TD>
<FONT face="宋体"></FONT>
</TR>
<TR>
<TD style="WIDTH: 111px">
<asp:Label id="Label2" runat="server">Code String</asp:Label></TD>
<TD>
<asp:TextBox id="txt_CodeString" runat="server" Width="440px" ReadOnly="True"></asp:TextBox></TD>
</TR>
<TR>
<TD style="WIDTH: 111px"><FONT face="宋体"></FONT></TD>
<TD>
<asp:Button id="btn_Encode" runat="server" Text="Encode"></asp:Button><FONT face="宋体"> </FONT>
<asp:Button id="btn_Clear" runat="server" Width="56px" Text="Decode"></asp:Button></TD>
</TR>
</TABLE>
</form>
</body>
</HTML>
后台页面:
后台页面#region 后台页面
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 Utility.Common;
namespace WebUI
{
/**//**//**//// <summary>
/// Encode 的摘要说明。
/// </summary>
public class Encode : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.TextBox txt_ClearString;
protected System.Web.UI.WebControls.TextBox txt_CodeString;
protected System.Web.UI.WebControls.Button btn_Encode;
protected System.Web.UI.WebControls.Button btn_Clear;
protected System.Web.UI.WebControls.Label Label2;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
Web 窗体设计器生成的代码Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/**//**//**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btn_Encode.Click += new System.EventHandler(this.btn_Encode_Click);
this.btn_Clear.Click += new System.EventHandler(this.btn_Clear_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btn_Encode_Click(object sender, System.EventArgs e)
{
Utility.Common.EncryptionUtil EU = new EncryptionUtil();
this.txt_CodeString.Text = EU.EncryptData(EU.key,this.txt_ClearString.Text.Trim());
}
private void btn_Clear_Click(object sender, System.EventArgs e)
{
Utility.Common.EncryptionUtil EU = new EncryptionUtil();
this.txt_ClearString.Text = EU.DecryptData(EU.key,this.txt_CodeString.Text.Trim());
}
}
}
#endregion