'==================================================
'this function encrypts a string
'==================================================
Function encryptData(strData)
	Dim vDLen
	Dim vChar
	Dim vString
	Dim vSEnc
	Dim k
	Dim a
	Dim z
	Dim vSecChar
	
	a = Second(Time())
	a = CLng(Right(Cstr(a),1))
	if a = 0 then
		a = 1
	end if
	
	z = CLng(Right(Cstr(Rnd()),1))
	if (CLng(CStr(a&z))< 32) then
		vSecChar = CStr(a&z)
	else
		vSecChar = Chr(CLng(CStr(a&z)))
	end if
	
	vString = vSecChar
	for k = 1 to Len(strData)
		vChar = Mid(strData,k,1)
		vSEnc = EncryptChar(vChar,a,z)
		vString = vString & vSEnc
	next
	encryptData = vString
End Function

'==================================================
'This function return an encrypted character
'==================================================
Function EncryptChar(byVal sChar, byVal x, byVal y)
	Dim vCharEnc
	Dim vAscii
	Dim vEnc
	Dim vDiff
	
	'group each by second value (x)
	Select Case x
		Case 1, 4, 7
			Select Case sChar
				Case "!", "$", "%", "x", "y", "~", " "
					vCharEnc = sChar
				Case Chr(34)
					vCharEnc = "}"
				Case "}"
					vCharEnc = Chr(34)
				Case "z"
					vCharEnc = "&"
				Case "&"
					vCharEnc = "z"
				Case "|"
					vCharEnc = "'"
				Case "'"
					vCharEnc = "|"
				Case "#"
					vCharEnc = "{"
				Case "{"
					vCharEnc = "#"
				Case Else
					vAscii = ASC(sChar)
					vEnc = vAscii + x
					if vEnc >= 120 then
						vDiff = 120 - vAscii
						vCharEnc = Chr(39 + vDiff)
					else
						vCharEnc = Chr(vAscii + x)
					end if
							
			End Select	
		Case 2, 5, 8
			Select Case sChar
				Case Chr(34), "}", "&", "|", "#", "{", " "
					vCharEnc = sChar
				Case "!"
					vCharEnc = "$"
				Case "$"
					vCharEnc = "!"
				Case "z"
					vCharEnc = "%"
				Case "%"
					vCharEnc = "z"
				Case "x"
					vCharEnc = "'"
				Case "'"
					vCharEnc = "x"
				Case "~"
					vCharEnc = "y"
				Case "y"
					vCharEnc = "~"
				Case Else
					vAscii = ASC(sChar)
					vEnc = vAscii + y
					if vEnc >= 120 then
						vDiff = 120 - vAscii
						vCharEnc = Chr(39 + vDiff)
					else
						vCharEnc = Chr(vAscii + y)
					end if
							
			End Select	
			
		Case Else
			Select Case sChar
				Case "~", "}", "!", "|", "%", "{", "z"
					vCharEnc = sChar
				Case "&"
					vCharEnc = "$"
				Case "$"
					vCharEnc = "&"
				Case " "
					vCharEnc = "#"
				Case "#"
					vCharEnc = " "
				Case "x"
					vCharEnc = "'"
				Case "'"
					vCharEnc = "x"
				Case Chr(34)
					vCharEnc = "y"
				Case "y"
					vCharEnc = Chr(34)
				Case Else
					vAscii = ASC(sChar)
					vEnc = vAscii + (x+y)
					if vEnc >= 120 then
						vDiff = 120 - vAscii
						vCharEnc = Chr(39 + vDiff)
					else
						vCharEnc = Chr(vAscii + x + y)
					end if
							
			End Select	
	End Select
	EncryptChar = vCharEnc
End Function

'===================================================
'This function decrypts data based from our defined
'encryption
'===================================================
Function decryptData(strData)
	Dim vFirstTwo
	Dim vDecStr
	DIm vSecCode
	
	Dim a
	Dim z
	Dim vDecLen
	Dim vOriData
	Dim itr
	
	Dim vOriChar
	Dim decChar
	
	Dim vDecStrx
	Dim n
	Dim vNum
	
	vNum = true
	vFirstTwo = Left(strData,2)
	If (IsNumeric(vFirstTwo)) then
		'determine for cases like (6+,+6,6-,-6)
		for n = 1 to Len(vFirstTwo)
			if not (IsNumeric(Mid(vFirstTwo,n,1))) then
				vNum = false
			end if
		next
		
		if vNum then
			vDecStr = Right(strData, Len(strData) - 2)
			vSecCode = Left(strData,2)
		else
			vDecStr = Right(strData, Len(strData) - 1)
			vSecCode = CStr(Asc(Left(strData,1)))
		end if
		
	else
		vDecStr = Right(strData,Len(strData) - 1)
		vSecCode = CStr(Asc(Left(strData,1)))
	end if
	
	a = Left(vSecCode,1)
	z = Mid(vSecCode,2,1)
	
	vDecLen = Len(vDecStr)
	vOriData = ""
	
	for itr = 1 to vDecLen
		decChar = Mid(vDecStr,itr,1)
		vOriChar = DecryptChar(decChar,a,z)
		vOriData = vOriData & vOriChar
	next
	decryptData = vOriData
End Function


'==================================================
'This function return a decrypted character
'==================================================
Function DecryptChar(byVal sChar, byVal x, byVal y)
	Dim vCharEnc
	Dim vAscii
	Dim vEnc
	Dim vDiff
	Dim vDec
	Dim vDec1
	
	'group each by second value (x)
	x = CLng(x)
	y = CLng(y)
	
	Select Case x
		Case 1, 4, 7
			Select Case sChar
				Case "!", "$", "%", "x", "y", "~", " "
					vCharEnc = sChar
				Case Chr(34)
					vCharEnc = "}"
				Case "}"
					vCharEnc = Chr(34)
				Case "z"
					vCharEnc = "&"
				Case "&"
					vCharEnc = "z"
				Case "|"
					vCharEnc = "'"
				Case "'"
					vCharEnc = "|"
				Case "#"
					vCharEnc = "{"
				Case "{"
					vCharEnc = "#"
				Case Else
					vAscii = ASC(sChar)
					vDec = CLng(vAscii) - 39
					if (CLng(vDec) <= CLng(x)) then
						vDec1 = 120 - CLng(vDec)
					else
						vDec1 = CLng(vAscii) - CLng(x)
					end if
					
					vCharEnc = Chr(vDec1)
			End Select	
		Case 2, 5, 8
			Select Case sChar
				Case Chr(34), "}", "&", "|", "#", "{", " "
					vCharEnc = sChar
				Case "!"
					vCharEnc = "$"
				Case "$"
					vCharEnc = "!"
				Case "z"
					vCharEnc = "%"
				Case "%"
					vCharEnc = "z"
				Case "x"
					vCharEnc = "'"
				Case "'"
					vCharEnc = "x"
				Case "~"
					vCharEnc = "y"
				Case "y"
					vCharEnc = "~"
				Case Else
					vAscii = ASC(sChar)
					
					vDec = CLng(vAscii) - 39
					if (CLng(vDec) <= CLng(y)) then
						vDec1 = 120 - CLng(vDec)
					else
						vDec1 = CLng(vAscii) - CLng(y)
					end if
					vCharEnc = Chr(vDec1)
				End Select	
			
		Case Else
			Select Case sChar
				Case "~", "}", "!", "|", "%", "{", "z"
					vCharEnc = sChar
				Case "&"
					vCharEnc = "$"
				Case "$"
					vCharEnc = "&"
				Case " "
					vCharEnc = "#"
				Case "#"
					vCharEnc = " "
				Case "x"
					vCharEnc = "'"
				Case "'"
					vCharEnc = "x"
				Case Chr(34)
					vCharEnc = "y"
				Case "y"
					vCharEnc = Chr(34)
				Case Else
					vAscii = ASC(sChar)
					vDec = CLng(vAscii) - 39
					if (CLng(vDec) <= CLng(x+y)) then
						vDec1 = 120 - CLng(vDec)
					else
						vDec1 = CLng(vAscii) - CLng(x+y)
					end if
					vCharEnc = Chr(vDec1)
			End Select	
	End Select
	DecryptChar = vCharEnc
End Function

'=====================================================================
'This function converts a string to its
'equivalent URL encode string
'=====================================================================
Function convert2URLEncode(byVal strWord)
	Dim strWordLen 'As Long
    Dim itr 'As Long
    Dim strConvertWord 'As String
    Dim strConvertChar 'As String
    Dim strCharv 'As String
    
    'initialize the values
    strConvertWord = ""
    strConvertChar = ""
    strChar = ""
    
    'get the length of the string
    strWordLen = Len(strWord)
    
    'loop throught its word and convert
    For itr = 1 To strWordLen
        'get each character in the string
        strCharv = Mid(strWord, itr, 1)
        'check if character is safe or not to load into browser
        If (checkIfIsUnsafe(strCharv)) Then
            'determine the length of hexadecimal
            'representation of each character
            'if less than 2 addzero
            If (Len(Hex(Asc(strCharv))) < 2) Then
                strConvertChar = "%0" & Hex(Asc(strCharv))
            Else
                strConvertChar = "%" & Hex(Asc(strCharv))
            End If
        Else
            strConvertChar = strCharv
        End If
        
        'concatenate each character string
        'to form the converted word
        strConvertWord = strConvertWord & strConvertChar
    Next
    
    'return the result
    convert2URLEncode = strConvertWord
End Function

'================================================================
'This function determines if the character
'is safe to load into browser or not
'this is needed for uRL encode function
'================================================================
Function checkIfIsUnsafe(ByVal strChar)
    Dim aschiicode' As Long
    Dim blnVal' As Boolean
    
    aschiicode = Asc(strChar)
    If ((aschiicode >= 0) And (aschiicode <= 47)) Then
        blnVal = True
    ElseIf ((aschiicode >= 58) And (aschiicode <= 64)) Then
        blnVal = True
    ElseIf ((aschiicode >= 91) And (aschiicode <= 96)) Then
        blnVal = True
    ElseIf ((aschiicode >= 123) And (aschiicode <= 255)) Then
        blnVal = True
    Else
        blnVal = False
    End If
    
    'return the value
    checkIfIsUnsafe = blnVal
    
End Function

