How To Decrypt an URL or String In VbScript or UFT

April 02, 2018

For encryption and decryption the best  options to avail to go to online site and transform what we need.
Few good sites could be as follows:
https://www.urldecoder.org/
https://meyerweb.com/eric/tools/dencoder/
https://urldecode.org/
https://codebeautify.org/url-decode-string and for base 64 decoding https://www.base64decode.org/
But I tried to understand what was th elogic behind it:
I searched google and found one link in StackOverflow.
https://stackoverflow.com/questions/4998715/does-vba-have-any-built-in-url-decoding
Mitch Wheat's Solution

Public Function URLDecode(ByVal strEncodedURL As String) As String
   Dim str As String
   str = strEncodedURL 
   If Len(str) > 0 Then
      str = Replace(str, "&amp", " & ")
      str = Replace(str, "&#03", Chr(39))
      str = Replace(str, "&quo", Chr(34))
      str = Replace(str, "+", " ")
      str = Replace(str, "%2A", "*")
      str = Replace(str, "%40", "@")
      str = Replace(str, "%2D", "-")
      str = Replace(str, "%5F", "_")
      str = Replace(str, "%2B", "+")
      str = Replace(str, "%2E", ".")
      str = Replace(str, "%2F", "/")

      URLDecode = str
  End If

End Function

This code was not perfect the writer of the code has given some idea how to do it. He has also mentioned one link for better approach- http://www.freevbcode.com/ShowCode.asp?ID=1512. But this is in VB. My objective is to get the same in VBscript so that I can work with UFT.

Markus Diersbock' solution:



Public Function URLDecode(sEncodedURL As String) As String

On Error GoTo Catch

Dim iLoop   As Integer
Dim sRtn    As String
Dim sTmp    As String

If Len(sEncodedURL) > 0 Then
    ' Loop through each char
    For iLoop = 1 To Len(sEncodedURL)
        sTmp = Mid(sEncodedURL, iLoop, 1)
        sTmp = Replace(sTmp, "+", " ")
        ' If char is % then get next two chars
        ' and convert from HEX to decimal
        If sTmp = "%" and LEN(sEncodedURL) + 1 > iLoop + 2 Then
            sTmp = Mid(sEncodedURL, iLoop + 1, 2)
            sTmp = Chr(CDec("&H" & sTmp))
            ' Increment loop by 2
            iLoop = iLoop + 2
        End If
        sRtn = sRtn & sTmp
    Next
    URLDecode = sRtn
End If

Finally:
    Exit Function
Catch:
    URLDecode = ""
    Resume Finally
End Function
But these solutions were not enough. I read in wiki about Percent encoding -
and the RFC 3986. I have also gone through the reserve character after percent encoding. When I was developing the same I found one more site with exactly the solution I was looking for: MarcusJT has provided the exact solution to implement the decode functionality in Vbscript. The code he has used is written below:

Function URLDecode(str)
str = unescape(str)
str = replace(str,"+"," ")
str = replace(str,"%2A","*")
str = replace(str,"%40","@")
str = replace(str,"%2D","-")
str = replace(str,"%5F","_")
str = replace(str,"%2B","+")
str = replace(str,"%2E",".")
str = replace(str,"%2F","/")
URLDecode = str
End Function

As per him, this functionality was given in Microsoft's Netscape JS in the form of escape() and unescape() function. There is a superior version available called server.URLEncode. VBscript's decode functionality is full of flaws. But this code served most of my URL or string decoding.

You Might Also Like

0 comments