解码加密的查询字符串

发布于 2024-12-25 05:41:35 字数 2068 浏览 2 评论 0原文

我正在使用下面LINK中描述的方法,并且我使用以下代码进行加密:

'Page1.aspx    
Protected Sub butEncrypt_Click(sender As Object, e As EventArgs) Handles butEncrypt.Click
    Dim QueryString As String = "type=Int&pk=" & _primaryKey
    QueryString = Tools.encryptQueryString(QueryString)
    Response.Redirect(/SearchResults.aspx?Val=" & QueryString)
End Sub

然后最后解密:

        'SearchResults.aspx
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If (Not IsPostBack) Then
         If Not String.IsNullOrEmpty(HttpContext.Current.Request(CIAppGlobals.GlobalVar.Val)) Then
            Dim qs As String = Request.QueryString(CIAppGlobals.GlobalVar.Val)
            qs = Tools.decryptQueryString(qs)

            Dim Values As String() = qs.Split(CChar("&"))

            _imageType = String.Empty
            _primaryKey = 0

            For Each value As String In Values
               Dim data As String() = value.Split(CChar("="))

               Select Case data(0).ToUpper
                  Case "TYPE"
                     _imageType = data(1)
                  Case "PK"
                     _primaryKey = CInt(data(1))
               End Select
            Next
            Else
               _imageType = HttpContext.Current.Request("type")
               _primaryKey = CInt(HttpContext.Current.Request("pk"))
         End If
    End If
   End Sub

我的问题是,除了我正在做的事情之外,我是否应该使用不同的方法来提取解码的查询字符串值?预先感谢您的建设性回应。

解决方案

在查看了 Darin 的回复后,我决定将其合并到我的项目中,这是我更新的代码:

'Page1.aspx    
Protected Sub butEncrypt_Click(sender As Object, e As EventArgs) Handles butEncrypt.Click
  Dim query = HttpUtility.ParseQueryString(String.Empty)
  query("type") = "Int"
  query("pk") = CStr(_primaryKey)

  Dim QueryString As String = Tools.encryptQueryString(query.ToString())
  Response.Redirect(/SearchResults.aspx?Val=" & QueryString)
End Sub

我仍然想加密查询字符串,因为我想防止用户手动更改查询字符串值

I am using the method described in the following LINK and I am using the following code to encrypt:

'Page1.aspx    
Protected Sub butEncrypt_Click(sender As Object, e As EventArgs) Handles butEncrypt.Click
    Dim QueryString As String = "type=Int&pk=" & _primaryKey
    QueryString = Tools.encryptQueryString(QueryString)
    Response.Redirect(/SearchResults.aspx?Val=" & QueryString)
End Sub

and then finally de-encrypt:

        'SearchResults.aspx
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If (Not IsPostBack) Then
         If Not String.IsNullOrEmpty(HttpContext.Current.Request(CIAppGlobals.GlobalVar.Val)) Then
            Dim qs As String = Request.QueryString(CIAppGlobals.GlobalVar.Val)
            qs = Tools.decryptQueryString(qs)

            Dim Values As String() = qs.Split(CChar("&"))

            _imageType = String.Empty
            _primaryKey = 0

            For Each value As String In Values
               Dim data As String() = value.Split(CChar("="))

               Select Case data(0).ToUpper
                  Case "TYPE"
                     _imageType = data(1)
                  Case "PK"
                     _primaryKey = CInt(data(1))
               End Select
            Next
            Else
               _imageType = HttpContext.Current.Request("type")
               _primaryKey = CInt(HttpContext.Current.Request("pk"))
         End If
    End If
   End Sub

My question is should I being using a different method to extract the decoded query string values other than what I am doing? Thanks in advance for your constructive responses.

Solution

After looking at Darin's response I have decided to incorporate it into my project, here is my updated code:

'Page1.aspx    
Protected Sub butEncrypt_Click(sender As Object, e As EventArgs) Handles butEncrypt.Click
  Dim query = HttpUtility.ParseQueryString(String.Empty)
  query("type") = "Int"
  query("pk") = CStr(_primaryKey)

  Dim QueryString As String = Tools.encryptQueryString(query.ToString())
  Response.Redirect(/SearchResults.aspx?Val=" & QueryString)
End Sub

I still want to encrypt the query string because I want to prevent users from changing the Query String Values manually

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

韬韬不绝 2025-01-01 05:41:35

您首先错误地构建了查询字符串。您正在使用字符串连接,但没有正确编码它们。如果 _primaryKey 包含 &= 字符怎么办?您可以使用 ParseQueryString 方法正确构建查询字符串:

Dim query = HttpUtility.ParseQueryString(String.Empty)
query("type") = "Int"
query("pk") = _primaryKey
Dim queryString = query.ToString()

相同方法可用于解析解码后的查询字符串:

Dim values = HttpUtility.ParseQueryString(qs)
Dim type = query("type")
Dim primaryKey = query("pk")
' work with the type and primaryKey values

处理 url 时切勿使用字符串连接和拆分。始终使用正确的工具完成正确的工作。

这就是创建/解析查询字符串所涉及的。就加密/解密值而言,您还没有向我们展示/告诉我们有关您正在使用的 Tools 类的任何信息,因此我无法向您提供任何有关它的建设性评论。

您知道最好的加密是永远不将实际值发送给客户端。因此,您可以将其存储在服务器上的某些后端存储中,然后在 url 中使用唯一的 id。该 id 可以在目标页面上使用来获取原始值。这样你就不需要加密/解密任何东西。

You are incorrectly building the query string in the first place. You are using string concatenations and not properly encoding them. What if _primaryKey contains a & or = characters? You could use the ParseQueryString method to properly build a query string:

Dim query = HttpUtility.ParseQueryString(String.Empty)
query("type") = "Int"
query("pk") = _primaryKey
Dim queryString = query.ToString()

The same method could be used for parsing the decoded query string:

Dim values = HttpUtility.ParseQueryString(qs)
Dim type = query("type")
Dim primaryKey = query("pk")
' work with the type and primaryKey values

Never use string concatenations and splitting when dealing with urls. Always use the right tool for the right job.

That's as far as creating/parsing query strings is concerned. As far as encrypting/decryption the values is concerned, you haven't shown/told us anything about the Tools class that you are using so I cannot provide you with any constructive comments about it.

You know that the best encryption is to never send the actual value to the client. So you could store it in some backend storage on the server and then use an unique id in the url. This id could be used on the target page to fetch the original value. This way you don't need to be encrypting/decrypting anything.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文