|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
我觉得很重要,一般所说的不重要应该指的是:你学好一种以后再学另一种就很容易了。(因为这样大家可能有一个错觉就是语言不是很重要,只要随便学一种就可以了,其实不是这样的。加密上面的代码完成了一个可逆加密的办法。能够用于对Cookie,QueryString等加密处置。
检察例子
VB.net代码
<%@PageLanguage="vb"AutoEventWireup="false"Codebehind="EncString.<ahref="http://dev.21tx.com/web/asp/"target="_blank">ASP</a>x.vb"
Inherits="aspx<ahref="http://dev.21tx.com/web/"target="_blank">Web</a>.EncString"%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.0Transitional//EN">
<HTML>
<HEAD>
<title>一个可逆加密的例子</title>
<metaname="GENERATOR"content="MicrosoftVisualStudio.NET7.0">
<metaname="CODE_LANGUAGE"content="VisualBasic7.0">
<metaname="vs_defaultClientScript"content="<ahref="http://dev.21tx.com/web/javascript/"target="_blank">JavaScript</a>">
<metaname="vs_targetSchema"content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<bodyMS_POSITIONING="GridLayout">
<asp:Labelid="Label1"runat="server"></asp:Label>
<palign="center">
<formid="Form1"method="post"runat="server">
<FONTface="宋体">
<asp:TextBoxid="TextBox1"runat="server"Width="96%"></asp:TextBox>
<asp:RadioButtonListid="RadioButtonList1"runat="server"Font-Bold="True"
RepeatDirection="Horizontal"AutoPostBack="True"OnSelectedIndexChanged="ShowRes">
</asp:RadioButtonList>
<asp:TextBoxid="TextBox2"runat="server"Width="96%"></asp:TextBox>
</FONT>
</form>
</p>
</body>
</HTML>
后端代码EncString.aspx.vb:
ImportsSystem
ImportsSystem.IO
ImportsSystem.Xml
ImportsSystem.Text
ImportsSystem.Security.Cryptography
PublicClassEncString
InheritsSystem.Web.UI.Page
ProtectedWithEventsTextBox1AsSystem.Web.UI.WebControls.TextBox
ProtectedWithEventsTextBox2AsSystem.Web.UI.WebControls.TextBox
ProtectedWithEventsForm1AsSystem.Web.UI.HtmlControls.HtmlForm
ProtectedWithEventsLabel1AsSystem.Web.UI.WebControls.Label
ProtectedWithEventsRadioButtonList1AsSystem.Web.UI.WebControls.RadioButtonList
#Region"WebFormDesignerGeneratedCode"
ThiscallisrequiredbytheWebFormDesigner.
<System.Diagnostics.DebuggerStepThrough()>PrivateSubInitializeComponent()
EndSub
PrivateSubPage_Init(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles
MyBase.Init
CODEGEN:ThismethodcallisrequiredbytheWebFormDesigner
Donotmodifyitusingthecodeeditor.
InitializeComponent()
EndSub
#EndRegion
PrivateSubPage_Load(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles
MyBase.Load
Putusercodetoinitializethepagehere
Label1.Text="<h3align=center>一个可逆加密的例子</h3>"
IfNotIsPostBackThen
DimMyListAsNewArrayList()
MyList.Add("加密")
MyList.Add("解密")
RadioButtonList1.DataSource=MyList
RadioButtonList1.DataBind()
EndIf
EndSub
加密
PublicSharedFunctionEncryptText(ByValstrTextAsString)AsString
ReturnEncrypt(strText,"&%#@?,:*")
EndFunction
解密
PublicSharedFunctionDecryptText(ByValstrTextAsString)AsString
ReturnDecrypt(strText,"&%#@?,:*")
EndFunction
加密函数
PrivateSharedFunctionEncrypt(ByValstrTextAsString,ByValstrEncrKeyAsString)AsString
DimbyKey()AsByte={}
DimIV()AsByte={&H12,&H34,&H56,&H78,&H90,&HAB,&HCD,&HEF}
Try
byKey=System.Text.Encoding.UTF8.GetBytes(Left(strEncrKey,8))
DimdesAsNewDESCryptoServiceProvider()
DiminputByteArray()AsByte=Encoding.UTF8.GetBytes(strText)
DimmsAsNewMemoryStream()
DimcsAsNewCryptoStream(ms,des.CreateEncryptor(byKey,IV),CryptoStreamMode.Write)
cs.Write(inputByteArray,0,inputByteArray.Length)
cs.FlushFinalBlock()
ReturnConvert.ToBase64String(ms.ToArray())
CatchexAsException
Returnex.Message
EndTry
EndFunction
解密函数
PrivateSharedFunctionDecrypt(ByValstrTextAsString,ByValsDecrKeyAsString)AsString
DimbyKey()AsByte={}
DimIV()AsByte={&H12,&H34,&H56,&H78,&H90,&HAB,&HCD,&HEF}
DiminputByteArray(strText.Length)AsByte
Try
byKey=System.Text.Encoding.UTF8.GetBytes(Left(sDecrKey,8))
DimdesAsNewDESCryptoServiceProvider()
inputByteArray=Convert.FromBase64String(strText)
DimmsAsNewMemoryStream()
DimcsAsNewCryptoStream(ms,des.CreateDecryptor(byKey,IV),CryptoStreamMode.Write)
cs.Write(inputByteArray,0,inputByteArray.Length)
cs.FlushFinalBlock()
DimencodingAsSystem.Text.Encoding=System.Text.Encoding.UTF8
Returnencoding.GetString(ms.ToArray())
CatchexAsException
Returnex.Message
EndTry
EndFunction
PublicSubShowRes(ByValsenderAsObject,ByValeAsSystem.EventArgs)_
HandlesRadioButtonList1.SelectedIndexChanged
IfRadioButtonList1.SelectedIndex=0Then
TextBox2.Text=EncryptText(TextBox1.Text)
Else
TextBox2.Text=DecryptText(TextBox1.Text)
EndIf
EndSub
EndClass
C#代码
EncryptString.aspx
<%@Pagelanguage="c#"EnableViewState="true"Codebehind="EncryptString.aspx.cs"AutoEventWireup="false"Inherits="eMeng.Exam.EncryptString"%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.0Transitional//EN">
<HTML>
<HEAD>
<title>一个可逆加密的例子</title>
<metaname="GENERATOR"content="MicrosoftVisualStudio.NET7.0">
<metaname="CODE_LANGUAGE"content="VisualBasic7.0">
<metaname="vs_defaultClientScript"content="JavaScript">
<metaname="vs_targetSchema"content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<formid="Form1"method="post"runat="server">
<palign="center">一个可逆加密的例子
<asp:TextBoxid="TextBox1"runat="server"Width="96%">http://dotnet.aspx.cc/</asp:TextBox>
<asp:RadioButtonListid="RadioButtonList1"runat="server"Font-Bold="True"RepeatDirection="Horizontal"
AutoPostBack="True"></asp:RadioButtonList>
<asp:TextBoxid="TextBox2"runat="server"Width="96%"></asp:TextBox>
<asp:TextBoxid="Textbox3"runat="server"Width="96%"></asp:TextBox>
</p>
</form>
</body>
</HTML>
EncryptString.aspx.cs
usingSystem;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Drawing;
usingSystem.Web;
usingSystem.Web.SessionState;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.HtmlControls;
usingSystem.IO;
usingSystem.Text;
usingSystem.Security.Cryptography;
namespaceeMeng.Exam
{
///<summary>
///EncryptString的择要申明。
///</summary>
publicclassEncryptString:System.Web.UI.Page
{
protectedSystem.Web.UI.WebControls.TextBoxTextBox1;
protectedSystem.Web.UI.WebControls.RadioButtonListRadioButtonList1;
protectedSystem.Web.UI.WebControls.TextBoxTextBox2;
protectedSystem.Web.UI.WebControls.TextBoxTextbox3;
protectedSystem.Web.UI.HtmlControls.HtmlFormForm1;
privatevoidPage_Load(objectsender,System.EventArgse)
{
//在此处安排用户代码以初始化页面
if(!this.IsPostBack)
{
ArrayListMyList=newArrayList();
MyList.Add("加密");
MyList.Add("解密");
RadioButtonList1.DataSource=MyList;
RadioButtonList1.DataBind();
}
}
#regionWeb窗体计划器天生的代码
overrideprotectedvoidOnInit(EventArgse)
{
//
//CODEGEN:该挪用是ASP.NETWeb窗体计划器所必须的。
//
InitializeComponent();
base.OnInit(e);
}
///<summary>
///计划器撑持所需的办法-不要利用代码编纂器修正
///此办法的内容。
///</summary>
privatevoidInitializeComponent()
{
this.RadioButtonList1.SelectedIndexChanged+=newSystem.EventHandler(this.RadioButtonList1_SelectedIndexChanged);
this.Load+=newSystem.EventHandler(this.Page_Load);
}
#endregion
privatevoidRadioButtonList1_SelectedIndexChanged(objectsender,System.EventArgse)
{
if(RadioButtonList1.SelectedIndex==0)
TextBox2.Text=EncryptText(TextBox1.Text);
else
Textbox3.Text=DecryptText(TextBox2.Text);
}
//加密
publicstringEncryptText(StringstrText)
{
returnEncrypt(strText,"&%#@?,:*");
}
//解密
publicStringDecryptText(StringstrText)
{
returnDecrypt(strText,"&%#@?,:*");
}
//加密函数
privateStringEncrypt(StringstrText,StringstrEncrKey)
{
Byte[]byKey={};
Byte[]IV={0x12,0x34,0x56,0x78,0x90,0xAB,0xCD,0xEF};
try
{
byKey=System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0,8));
DESCryptoServiceProviderdes=newDESCryptoServiceProvider();
Byte[]inputByteArray=Encoding.UTF8.GetBytes(strText);
MemoryStreamms=newMemoryStream();
CryptoStreamcs=newCryptoStream(ms,des.CreateEncryptor(byKey,IV),CryptoStreamMode.Write);
cs.Write(inputByteArray,0,inputByteArray.Length);
cs.FlushFinalBlock();
returnConvert.ToBase64String(ms.ToArray());
}
catch(Exceptionex)
{
returnex.Message;
}
}
//解密函数
privateStringDecrypt(StringstrText,StringsDecrKey)
{
Byte[]byKey={};
Byte[]IV={0x12,0x34,0x56,0x78,0x90,0xAB,0xCD,0xEF};
Byte[]inputByteArray=newbyte[strText.Length];
try
{
byKey=System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0,8));
DESCryptoServiceProviderdes=newDESCryptoServiceProvider();
inputByteArray=Convert.FromBase64String(strText);
MemoryStreamms=newMemoryStream();
CryptoStreamcs=newCryptoStream(ms,des.CreateDecryptor(byKey,IV),CryptoStreamMode.Write);
cs.Write(inputByteArray,0,inputByteArray.Length);
cs.FlushFinalBlock();
System.Text.Encodingencoding=System.Text.Encoding.UTF8;
returnencoding.GetString(ms.ToArray());
}
catch(Exceptionex)
{
returnex.Message;
}
}
}
}
我以前很喜欢Serv-U,自从它用Java重写之后我就再也没用过,实在是太慢了,我宁可用IIS搭建FTP,虽然IIS搭建FTP在权限管理上很不灵活。 |
|