ASP Custom Functions

Soldato
Joined
8 Oct 2005
Posts
4,184
Location
Midlands, UK
Hi,

Im looking for a kind of library of useful asp custom functions i can use. E.g. to MD5 a string etc etc.

Thanks
 
I don't have anything for MD5, just Base64 -

Code:
	' =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= \/ \/ \/ \/ =-
	'	BASE64 ENCRYPTION!
	' =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
		const BASE_64_MAP_INIT ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
		const C_ENCODE_KEY = "mDf9O3"
		dim nl
		   dim Base64EncMap(63)
		   dim Base64DecMap(127)
		initCodecs
	
	
		' Encode - ENCODE THE QUERYSTRING ====================================================
		public function Encode(p_QS)
			Encode = base64Encode(SimpleXor(p_QS, C_ENCODE_KEY))
		end function
		' ======================================================================================
		
	
	
		' Decode - DECODE THE QUERYSTRING ====================================================
		public function Decode(p_QS)
			Decode = SimpleXor(Base64Decode(p_QS), C_ENCODE_KEY)			
		end function
		' ======================================================================================

	
	
		' INITCODECS - CREATE THE ARRAYS FOR ENCODING ==========================================
		public sub initCodecs()
			nl = "<P>" & chr(13) & chr(10)
			dim max, idx
			max = len(BASE_64_MAP_INIT)
			for idx = 0 to max - 1
			     Base64EncMap(idx) = mid(BASE_64_MAP_INIT, idx + 1, 1)
			next
			for idx = 0 to max - 1
			     Base64DecMap(ASC(Base64EncMap(idx))) = idx
			next
		end sub
		' ======================================================================================

	
	
		' ENCODE THE STRING ====================================================================
		public function base64Encode(plain)

			call initCodecs

		     if len(plain) = 0 then
		          base64Encode = ""
		          exit function
		     end if

		     dim ret, ndx, by3, first, second, third
		     by3 = (len(plain) \ 3) * 3
		     ndx = 1
		     do while ndx <= by3
		          first  = asc(mid(plain, ndx+0, 1))
		          second = asc(mid(plain, ndx+1, 1))
		          third  = asc(mid(plain, ndx+2, 1))
		          ret = ret & Base64EncMap(  (first \ 4) AND 63 )
		          ret = ret & Base64EncMap( ((first * 16) AND 48) + ((second \ 16) AND 15 ) )
		          ret = ret & Base64EncMap( ((second * 4) AND 60) + ((third \ 64) AND 3 ) )
		          ret = ret & Base64EncMap( third AND 63)
		          ndx = ndx + 3
		     loop
		     ' check for stragglers
		     if by3 < len(plain) then
		          first  = asc(mid(plain, ndx+0, 1))
		          ret = ret & Base64EncMap(  (first \ 4) AND 63 )
		          if (len(plain) MOD 3 ) = 2 then
		               second = asc(mid(plain, ndx+1, 1))
		               ret = ret & Base64EncMap( ((first * 16) AND 48) +((second \16) AND 15 ) )
		               ret = ret & Base64EncMap( ((second * 4) AND 60) )
		          else
		               ret = ret & Base64EncMap( (first * 16) AND 48)
		               ret = ret & "="
		          end if
		          ret = ret & "="
		     end if

		     base64Encode = ret
		end function
		' ======================================================================================

	
	
		' DECODE THE STRING ====================================================================
		public function base64Decode(scrambled)
		     if len(scrambled) = 0 then
		          base64Decode = ""
		          exit function
		     end if

		     ' ignore padding
		     dim realLen
		     realLen = len(scrambled)
		     do while mid(scrambled, realLen, 1) = "="
		          realLen = realLen - 1
		     loop
		     do while instr(scrambled," ")<>0
		         scrambled=left(scrambled,instr(scrambled," ")-1) & "+" & mid(scrambled,instr(scrambled," ")+1)
		     loop
		     dim ret, ndx, by4, first, second, third, fourth
		     ret = ""
		     by4 = (realLen \ 4) * 4
		     ndx = 1
		     do while ndx <= by4
		          first  = Base64DecMap(asc(mid(scrambled, ndx+0, 1)))
		          second = Base64DecMap(asc(mid(scrambled, ndx+1, 1)))
		          third  = Base64DecMap(asc(mid(scrambled, ndx+2, 1)))
		          fourth = Base64DecMap(asc(mid(scrambled, ndx+3, 1)))
		          ret = ret & chr( ((first * 4) AND 255) +   ((second \ 16) AND 3))
		          ret = ret & chr( ((second * 16) AND 255) + ((third \ 4) AND 15) )
		          ret = ret & chr( ((third * 64) AND 255) +  (fourth AND 63) )
		          ndx = ndx + 4
		     loop
		     ' check for stragglers, will be 2 or 3 characters
		     if ndx < realLen then
		          first  = Base64DecMap(asc(mid(scrambled, ndx+0, 1)))
		          second = Base64DecMap(asc(mid(scrambled, ndx+1, 1)))
		          ret = ret & chr( ((first * 4) AND 255) +   ((second \ 16) AND 3))
		          if realLen MOD 4 = 3 then
		               third = Base64DecMap(asc(mid(scrambled,ndx+2,1)))
		               ret = ret & chr( ((second * 16) AND 255) + ((third \ 4) AND 15) )
		          end if
		     end if

		     base64Decode = ret
		end function
		' ======================================================================================

	
	
		' XOR ENCRYPTION =======================================================================
		public function SimpleXor(InString,Key)
		    dim myIN, myKEY, myC, myPub
		    dim Keylist()
			    
		    myIN = InString
		    myKEY = Key
			    
		    redim KeyList(len(myKEY))
			    
		    i = 1
		    do while i<=len(myKEY)
		        KeyList(i) = Asc(Mid(myKEY, i, 1))
		        i = i + 1
		    loop       
			    
		    j = 1
		    i = 1
		    do while i<=len(myIn)
		        myC = myC & Chr(Asc(Mid(myIN, i, 1)) Xor KeyList(j))
		        i = i + 1
		        if j = len(myKEY) then j = 0
		        j = j + 1
		    loop
			 
		    SimpleXor = myC
		end function
		' ======================================================================================

	
	
		' GET TOKEN FROM ENCODED STRING *LOCAL* ================================================
		public function getToken(p_String, p_Token)
			dim arrString, p_Output, p_Data
			
			arrString = split(p_String, "&")
			for iStringLoop = 0 to ubound(arrString)
				arrValue = split(arrString(iStringLoop), "=")
				if cstr(arrValue(0)) = cstr(p_Token) then
					p_Output = arrValue(1)
					exit for
				end if
			next
			
			getToken = p_Output
		end function
		' ======================================================================================
	' =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-	
	'	BASE64 ENCRYPTION!
	' =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= /\ /\ /\ /\ =-

And this is how you use it..

Code:
' send encoded data
response.redirect("nextpage.asp?" & encode("name=Jimmy&surname=Jones&company=tesco")

' read encoded data
szData = decode(request.querystring)

szName = getToken(szData, "name")
szSurname = getToken(szData, "surname")
szCompany = getToken(szData, "company")
 
Last edited:

Hi Spunky,

I think I'm missing something blatently obvious here I can't retreive the querstring values like in your example. E.g. on page one I have a simple link:

Code:
<a href="<%="enc2.asp?" & encode("name=Johnny&surname=Vegas&company=OCuK")%>">Encoded QueryString</a>

on enc2.asp:

Code:
szData = decode(request.querystring)

szName = getToken(szData, "name")
szSurname = getToken(szData, "surname")
szCompany = getToken(szData, "company")
 
 response.write("Name = " & szName)
 response.write("Surname = " & szSurname)
 response.write("Company = " & szCompany)

It encodes the querystring fine and i am able to display the full decoded querystring fine on the second page, just not get the actual values from it.

Any ideas?

Cheers
 
Hi Suarve,

You're right that code above wasn't quite right - replace the previous getToken function with this one and it should work properly...

Code:
		' GET TOKEN FROM ENCODED STRING *LOCAL* ================================================
		public function getToken(p_String, p_Token)
			dim arrString, p_Output, p_Data
			
			arrString = split(p_String, "&")
			for iStringLoop = 0 to ubound(arrString)
				arrValue = split(arrString(iStringLoop), "=")
				if cstr(arrValue(0)) = cstr(p_Token) then
					p_Output = arrValue(1)
					exit for
				end if
			next
			
			getToken = p_Output
		end function
		' ======================================================================================

I've updated the original post with this fix too :)
 
Back
Top Bottom