最近做一个论坛入口时要实现帐号和密码不在IE地址栏出现而做的
index.aspx.cs (加密处理)
Byte[]Iv64={11,22,33,44,55,66,77,85};
Byte[]byKey64={10,20,30,40,50,60,70,80};
publicstringEncrypt(stringstrText)
{
try
{
DESCryptoServiceProviderdes=newDESCryptoServiceProvider();
Byte[]inputByteArray=Encoding.UTF8.GetBytes(strText);
MemoryStreamms=newMemoryStream();
CryptoStreamcs=newCryptoStream(ms,des.CreateEncryptor(byKey64,Iv64),
CryptoStreamMode.Write); cs.Write(inputByteArray,0,inputByteArray.Length); cs.FlushFinalBlock(); returnConvert.ToBase64String(ms.ToArray()); } catch(Exceptionex) { returnex.Message; } }
privatevoidbtnLogin_Click(objectsender,System.Web.UI.ImageClickEventArgse) { DateTimenowTime=DateTime.Now; stringpostUser=txtUser.Text.ToString(); stringpostPass=txtPassword.Text.ToString(); Response.Redirect("Login.aspx?clubID="+Encrypt(postUser+","+postPass+", "+nowTime.ToString())); }
login.aspx.cs (解密处理)
CryptoStreamMode.Write); cs.Write(inputByteArray,0,inputByteArray.Length); cs.FlushFinalBlock(); returnConvert.ToBase64String(ms.ToArray()); } catch(Exceptionex) { returnex.Message; } }
privatevoidbtnLogin_Click(objectsender,System.Web.UI.ImageClickEventArgse) { DateTimenowTime=DateTime.Now; stringpostUser=txtUser.Text.ToString(); stringpostPass=txtPassword.Text.ToString(); Response.Redirect("Login.aspx?clubID="+Encrypt(postUser+","+postPass+", "+nowTime.ToString())); }
//随机选8个字节既为密钥也为初始向量
Byte[]byKey64={10,20,30,40,50,60,70,80};
Byte[]Iv64={11,22,33,44,55,66,77,85};
publicstringDecrypt(stringstrText)
{
Byte[]inputByteArray=newbyte[strText.Length];
try
{
DESCryptoServiceProviderdes=newDESCryptoServiceProvider();
inputByteArray=Convert.FromBase64String(strText);
MemoryStreamms=newMemoryStream();
CryptoStreamcs=newCryptoStream(ms,des.CreateDecryptor(byKey64,Iv64),
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;
}
}
privatevoidPage_Load(objectsender,System.EventArgse)
{
if(Request.Params["clubID"]!=null)
{
stringoriginalValue=Request.Params["clubID"];
originalValue=originalValue.Replace("","+");
//+号通过url传递变成了空格。
stringdecryptResult=Decrypt(originalValue);
//DecryptString(string)解密字符串
stringdelimStr=",";
char[]delimiterArray=delimStr.ToCharArray();
string[]userInfoArray=null;
userInfoArray=decryptResult.Split(delimiterArray);
stringuserName=userInfoArray[0];
UseruserToLogin=newUser();
userToLogin.Username=userInfoArray[0];
userToLogin.Password=userInfoArray[1];
......
}
}