ASP learning: urldecode method Addendum

Source: Internet
Author: User

There is no urldecode function in ASP, as if there is a function in aspx. I am not sure, but it is still used in asp. You can find the urldecode function written by someone else on the Internet. However, this function is incorrect and difficult to understand in some aspects. In addition, an error occurs when there are uncommon double-byte characters in the text. For example, if "bytes" is set to "% 81W" after urlencoder, decoding fails.
In fact, dual-byte encoding only requires "W" to be compiled into a hexadecimal ASC code.
Knowledge point:The cookies in the computer are also encoded by urlencode, so urldecode is also useful for cracking cookies.
The source code is as follows:
Function URLDecode (enStr)
Dim deStr
Dim c, I, v
DeStr = ""
For I = 1 to len (enStr)
C = Mid (enStr, I, 1)
If c = "%" then
V = eval ("& h" + Mid (enStr, I + 1, 2 ))
If v <128 then
DeStr = deStr & chr (v)
I = I + 2
Else
If isvalidhex (mid (enstr, I, 3) then
If isvalidhex (mid (enstr, I + 3, 3) then
V = eval ("& h" + Mid (enStr, I + 1, 2) + Mid (enStr, I + 4, 2 ))
DeStr = deStr & chr (v)
I = I + 5
Else
V = eval ("& h" + Mid (enStr, I + 1, 2) + cstr (hex (asc (Mid (enStr, I + 3, 1 )))))
DeStr = deStr & chr (v)
I = I + 3
End if
Else
Destr = destr & c
End if
End if
Else
If c = "+" then
DeStr = deStr &""
Else
DeStr = deStr & c
End if
End if
Next
URLDecode = deStr
End function
Function isvalidhex (str)
Isvalidhex = true
Str = ucase (str)
If len (str) <> 3 then isvalidhex = false: exit function
If left (str, 1) <> "%" then isvalidhex = false: exit function
C = mid (str, 2, 1)
If not (c> = "0") and (c <= "9") or (c> = "") and (c <= "Z") then isvalidhex = false: exit function
C = mid (str, 3,1)
If not (c> = "0") and (c <= "9") or (c> = "") and (c <= "Z") then isvalidhex = false: exit function
End function
You can use this method to decode "% 81W.
Of course, you can also play with a little bit to make it your own string encryption method.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.