How to encrypt Query String parameters using Hexadecimal?

when you need to encrypt query string parameters in your URLs to secure your web applications.

1.Used hexadecimal to encode and decoding.
2.First Convert the Query string value into bytes.
3.Bytes converted into Hexadecimal value and pass to the query string.

Dim str As String = TextTool.ByteToHexString(Encoding.UTF8.GetBytes("Testing!"))
Response.Redirect("GettingQueryString.aspx?Msg=" & str)

ByteToHexString Function

Public Shared Function ByteToHexString(ByVal btyDataArray As Byte()) As String
Dim strDecodedData As String = String.Empty
Dim hexDigit() As Char = { _
"0"c, "1"c, "2"c, "3"c, "4"c, "5"c, "6"c, "7"c, _
"8"c, "9"c, "A"c, "B"c, "C"c, "D"c, "E"c, "F"c}
For ii As Integer = 0 To btyDataArray.Length - 1 Step 1
Dim b As Byte = btyDataArray(ii)
Dim c_array() As Char = {hexDigit((b >> 4) And &HF), hexDigit(b And &HF)}
strDecodedData = strDecodedData + New String(c_array)
Return strDecodedData
End Function

Again need to convert the hexadecimal value to string.

Dim str1 As String = TextTool.HexToString(Request.QueryString("Msg").ToString())
Response.Write("encode->" & str1)

HexToString function

Public Shared Function HexToString(ByVal strDataToDecode As String) As String
Dim data() As Byte
Dim dataArray As Char() = strDataToDecode.ToLower.ToCharArray
Dim nBytes As Integer = dataArray.Length()
ReDim data(nBytes / 2 - 1)

Dim i As Integer
Dim objCol As New Hashtable
For i = 0 To 15 Step 1
objCol.Add(Hex(i).ToLower, i)
Dim jj, ii, intData1, intData2 As Integer
jj = 0
For ii = 0 To nBytes - 1 Step 2
intData1 = CType(objCol.Item(dataArray(ii).ToString.Trim), Integer)
intData2 = CType(objCol.Item(dataArray(ii + 1).ToString.Trim), Integer)
data(jj) = intData1 * 16 + intData2
jj += 1
Dim strDecodedData As String = Encoding.UTF8.GetString(data)
Return strDecodedData
End Function


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: