对Url传输参数进行加密和解密
2008-11-12 16:45
573 查看
最近做一个论坛入口时要实现帐号和密码不在IE地址栏出现而做的
index.aspx.cs (加密处理)
Byte[] Iv64={11, 22, 33, 44, 55, 66, 77, 85};
Byte[] byKey64={10, 20, 30, 40, 50, 60, 70, 80};
public string Encrypt(string strText)
{
try
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
Byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey64, Iv64),
CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
catch(Exception ex)
{
return ex.Message;
}
}
private void btnLogin_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
DateTime nowTime = DateTime.Now;
string postUser = txtUser.Text.ToString();
string postPass = 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};
public string Decrypt(string strText)
{
Byte[] inputByteArray = new byte[strText.Length];
try
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey64, Iv64),
CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
return encoding.GetString(ms.ToArray());
}
catch(Exception ex)
{
return ex.Message;
}
}
private void Page_Load(object sender, System.EventArgs e)
{
if(Request.Params["clubID"]!=null)
{
string originalValue = Request.Params["clubID"];
originalValue = originalValue.Replace(" ","+");
//+号通过url传递变成了空格。
string decryptResult = Decrypt(originalValue);
//DecryptString(string)解密字符串
string delimStr = ",";
char[] delimiterArray = delimStr.ToCharArray();
string [] userInfoArray = null;
userInfoArray = decryptResult.Split(delimiterArray);
string userName = userInfoArray[0];
User userToLogin = new User();
userToLogin.Username = userInfoArray[0];
userToLogin.Password = userInfoArray[1];
......
}
}
index.aspx.cs (加密处理)
Byte[] Iv64={11, 22, 33, 44, 55, 66, 77, 85};
Byte[] byKey64={10, 20, 30, 40, 50, 60, 70, 80};
public string Encrypt(string strText)
{
try
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
Byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey64, Iv64),
CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
catch(Exception ex)
{
return ex.Message;
}
}
private void btnLogin_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
DateTime nowTime = DateTime.Now;
string postUser = txtUser.Text.ToString();
string postPass = 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};
public string Decrypt(string strText)
{
Byte[] inputByteArray = new byte[strText.Length];
try
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey64, Iv64),
CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
return encoding.GetString(ms.ToArray());
}
catch(Exception ex)
{
return ex.Message;
}
}
private void Page_Load(object sender, System.EventArgs e)
{
if(Request.Params["clubID"]!=null)
{
string originalValue = Request.Params["clubID"];
originalValue = originalValue.Replace(" ","+");
//+号通过url传递变成了空格。
string decryptResult = Decrypt(originalValue);
//DecryptString(string)解密字符串
string delimStr = ",";
char[] delimiterArray = delimStr.ToCharArray();
string [] userInfoArray = null;
userInfoArray = decryptResult.Split(delimiterArray);
string userName = userInfoArray[0];
User userToLogin = new User();
userToLogin.Username = userInfoArray[0];
userToLogin.Password = userInfoArray[1];
......
}
}
相关文章推荐
- WEB系统中加密\解密URL传输参数
- PHP将URL地址参数进行加密传输提高网站安全性
- WEB系统中加密\解密URL传输参数.
- WEB系统中加密解密URL传输参数.
- WEB系统中加密/解密URL传输参数
- WEB系统中加密\解密URL传输参数
- 对url参数后的value进行 加密解密
- 今天给大家介绍一下SpringBoot框架中URL参数如何进行Base64加密解密
- 分享如何使用PHP将URL地址参数进行加密传输提高网站安全性
- WEB系统中加密\解密URL传输参数(EIP.Framework中的实现)
- 分享如何使用PHP将URL地址参数进行加密传输提高网站安全性
- SpringBoot框架中URL参数如何进行Base64加密解密
- js 对 URL 参数进行 加密 解密
- 学习URL传输参数加密解密
- 使用Spring AOP对url参数进行解密以及对返回结果进行加密
- 分享如何使用PHP将URL地址参数进行加密传输提高网站安全性
- WEB系统中加密/解密URL传输参数.
- URL地址参数进行加密传输提高网站安全性
- 学习URL传输参数加密解密
- javascript将url中的参数加密解密代码