|
Class Base64class REM Const Dim sbase_64_characters ' Conversion Code Dim lenstring ' Computes the length of a string Dim icount ' Counter Dim ReturnValue ' return Value Dim Tempchar ' Cached characters Dim tempstring ' Cached String Dim paramstring ' Parameter String Dim Temhex ' Cache Cache hex Dim Templow ' Cache Low Dim Temphigh ' Cache High Dim mod3string ' Dim mod4string ' Dim Tempbinary ' Dim Tempbyteone ' Dim Tempbytetwo ' Dim Tempbytethree ' Dim Tempbytefour ' Dim Tempsavebitsone ' Dim Tempsavebitstwo ' '******************************************** ' Begin initializing Class '******************************************** Private Sub Class_Initialize () Sbase_64_characters = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/" End Sub '******************************************** ' End Initialization class ' ********************************************'******************************************** ' Begin destroying class '******************************************** Private Sub Class_Terminate () Sbase_64_characters= "" End Sub '******************************************** ' End Destroy class '******************************************** '******************************************** ' Begin BASE64 encoding an ANSI-encoded string '******************************************** Public Function Encode (paramstring) Tempstring= "" Returnvalue= "" Lenstring=len (paramstring) If Lenstring<1 Then Encode=returnvalue Else Mod3string=lenstring MoD 3 ' The number of complements is for ease of calculation. If Mod3string>0 Then Lenstring=lenstring+3-mod3string Lenstring=lenstring-3 End If
' *************************begin For Icount=1 to lenstring step 3 Tempbinary = Mid (paramstring, icount, 3) ' Response.Write tempbinary Tempbyteone= ASC (Mid (tempbinary, 1, 1)): Tempsavebitsone = Tempbyteone and 3 Tempbytetwo = ASC (Mid (Tempbinary, 2, 1)): Tempsavebitstwo = Tempbytetwo and 15 Tempchar = ASC (Mid (Tempbinary, 3, 1)) Tempbyteone = Mid (Sbase_64_characters, (Tempbyteone and 252) \ 4) + 1, 1) Tempbytetwo = Mid (Sbase_64_characters, (Tempbytetwo and R) and &HFF) + 1, 1) Tempbytethree = Mid (Sbase_64_characters, (Tempchar and) Or (Tempsavebitstwo * 4) and &HFF) + 1, 1) Tempbytefour = Mid (Sbase_64_characters, (Tempchar and 63) + 1, 1) tempstring = Tempbyteone & tempbytetwo & tempbytethree & tempbytefour Returnvalue=returnvalue & TempStrin G Next ' *************************end ' *************************begin processing the last remaining few characters If Mod3string>0 Then Tempbinary = Mid (paramstring, icount, mod3string) If Mod3string=1 Then tempstring = tempbinary & Chr ($) & Chr ($) & CHR (64) ' with the @ number of digits else tempstring = tempbinary & Chr ($) & CHR (64) ' complement digits with @ End If Returnvalue=returnvalue & TempString End If ' *************************end processing the last remaining few characters Encode=returnvalue End If End function '******************************************** ' End BASE64 encoding an ANSI-encoded string '********************************************'******************************************** ' End converts a Base64 encoded string into an ANSI-encoded string '******************************************** Public Function Decode (paramstring) Tempstring= "" Returnvalue= "" Lenstring=len (paramstring) If Lenstring<1 Then Decode=returnvalue Else Mod4string=lenstring MoD 4 If mod4string >0 then ' string length should be a multiple of 4 Decode=returnvalue Else ' begin to judge whether the @ number If Mid (paramstring, lenString-1, 1) = "@" Then mod4string=2 End If If Mid (paramstring, lenString-2, 1) = "@" Then Mod4string=1 End If ' End judgment is not the @ number If Mod4string>0 Then Lenstring=lenstring-4 End If
For Icount=1 to lenstring step 4 tempstring = Mid (paramstring, icount, 4) Tempbyteone = InStr (Sbase_64_characters, Mid (tempstring, 1, 1))-1 Tempbytetwo = InStr (Sbase_64_characters, Mid (tempstring, 2, 1))-1 Tempbytethree = InStr (Sbase_64_characters, Mid (tempstring, 3, 1))-1 Tempbytefour = InStr (Sbase_64_characters, Mid (tempstring, 4, 1))-1 Tempbyteone = Chr ((tempbytetwo) or (Tempbyteone * 4) and &HFF) Tempbytetwo = "" & Chr ((Tempbytethre E and) \ 4) Or (Tempbytetwo *) and &HFF) Tempbytethree = Chr ((Tempbytethree and 3) *) and &HFF) Or (Tempbytefour and 63)) Tempstring=tempbyteone & Tempbytetwo & Tempbytethree Returnvalue=returnvalue & TempString Next ' ******************************end' Handle the last remaining few characters If mod4string > 0 Then Tempstring=left (Right (paramstring,4), mod4string) returnvalue = returnvalue & tempstring End If Decode=returnvalue End If End If End Function '******************************************** ' End converts a Base64 encoded string into an ANSI-encoded string '******************************************** End Class |