26 Ağustos 2008 Salı

ASP Base64 Decode / Encode

' Functions to provide encoding/decoding of strings with Base64.' ' Encoding: myEncodedString = base64_encode( inputString )' Decoding: myDecodedString = base64_decode( encodedInputString )'' Programmed by Markus Hartsmar for ShameDesigns in 2002. ' Email me at: mark@shamedesigns.com' Visit our website at: http://www.shamedesigns.com/'Dim Base64CharsBase64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" & _"abcdefghijklmnopqrstuvwxyz" & _"0123456789" & _"+/"' Functions for encoding string to Base64Public Function base64_encode( byVal strIn )Dim c1, c2, c3, w1, w2, w3, w4, n, strOutFor n = 1 To Len( strIn ) Step 3c1 = Asc( Mid( strIn, n, 1 ) )c2 = Asc( Mid( strIn, n + 1, 1 ) + Chr(0) )c3 = Asc( Mid( strIn, n + 2, 1 ) + Chr(0) )w1 = Int( c1 / 4 ) : w2 = ( c1 And 3 ) * 16 + Int( c2 / 16 )If Len( strIn ) >= n + 1 Then w3 = ( c2 And 15 ) * 4 + Int( c3 / 64 ) Else w3 = -1End IfIf Len( strIn ) >= n + 2 Then w4 = c3 And 63 Else w4 = -1End IfstrOut = strOut + mimeencode( w1 ) + mimeencode( w2 ) + _mimeencode( w3 ) + mimeencode( w4 )Nextbase64_encode = strOutEnd FunctionPrivate Function mimeencode( byVal intIn )If intIn >= 0 Then mimeencode = Mid( Base64Chars, intIn + 1, 1 ) Else mimeencode = ""End IfEnd Function ' Function to decode string from Base64Public Function base64_decode( byVal strIn )Dim w1, w2, w3, w4, n, strOutFor n = 1 To Len( strIn ) Step 4w1 = mimedecode( Mid( strIn, n, 1 ) )w2 = mimedecode( Mid( strIn, n + 1, 1 ) )w3 = mimedecode( Mid( strIn, n + 2, 1 ) )w4 = mimedecode( Mid( strIn, n + 3, 1 ) )If w2 >= 0 Then _strOut = strOut + _Chr( ( ( w1 * 4 + Int( w2 / 16 ) ) And 255 ) )If w3 >= 0 Then _strOut = strOut + _Chr( ( ( w2 * 16 + Int( w3 / 4 ) ) And 255 ) )If w4 >= 0 Then _strOut = strOut + _Chr( ( ( w3 * 64 + w4 ) And 255 ) )Nextbase64_decode = strOutEnd FunctionPrivate Function mimedecode( byVal strIn )If Len( strIn ) = 0 Then mimedecode = -1 : Exit FunctionElsemimedecode = InStr( Base64Chars, strIn ) - 1End IfEnd Function

Hiç yorum yok: