对Url传输参数进行加密和解密

最近做一个论坛入口时要实现帐号和密码不在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 (解密处理)


//随机选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]; ...... } }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值