我已经阅读了很多关于如何加密和解密查询字符串的文章,但似乎无法找到关于如何在html标记中使用它的任何文章。这就是我想要实现的目标 产品ID是一个整数,但我不想将它发送到SingleProduct.aspx页面。我想对它进行加密,然后在页面上对其进行解密,以便将其用于其他操作
<a href="Singleproduct.aspx?Product=<%#Eval("Product_Id")) %>">
答案 0 :(得分:1)
i use user's id (Session["UserId"]) as my encrypt key. ref RijndaelManaged Class
productlist.aspx like
<a href="Singleproduct.aspx?enProduct=<%#EncodeId(Eval("id")) %>">En Product Detail</a>
productlist.aspx.cs like
protected void Page_Load(object sender, EventArgs e)
{
Session["UserId"] = "rainmaker";
}
protected string EncodeId(object id)
{
var encryptKey = (string)Session["UserId"];
var encryptKeyArray = Encoding.ASCII.GetBytes(encryptKey);
Array.Resize(ref encryptKeyArray, 16);
// Encrypt the string to an array of bytes.
byte[] encrypted = EncryptStringToBytes(Convert.ToString(id), encryptKeyArray, encryptKeyArray);
string encryptedStr = Convert.ToBase64String(encrypted).Replace('+', '-').Replace('/', '_');
return encryptedStr;
}
Singleproduct.aspx.cs Decrypt by Session["UserId"]
var enProductId = Request.QueryString["enProduct"];
if (enProductId != null)
{
var encryptKey = (string)Session["UserId"];
var encryptKeyArray = Encoding.ASCII.GetBytes(encryptKey);
Array.Resize(ref encryptKeyArray, 16);
var encryptedArray = Convert.FromBase64String(enProductId.Replace('_', '/').Replace('-', '+'));
// Decrypt the bytes to a string.
string id = DecryptStringFromBytes(encryptedArray, encryptKeyArray, encryptKeyArray);
Response.Write(id);
}
Alternatively, you can add a GUID field for link to detail id.