| Base64 encoding and decoding Functions This was edited by myself after I read several base64 encoding and decoding functions. Because VBScript in the Chinese operating system uses the Unicode Character Set Many base64 encoding and decoding functions are theoretically correct, but they cannot be run! File Name base64test. asp <% Sbase_64_characters = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789 + /" Sbase_64_characters = strunicode2ansi (sbase_64_characters) Function strunicodelen (ascontents) 'Calculate the ANSI encoding length of the Unicode string Ascontents1 = "A" & ascontents Len1 = Len (ascontents1) K = 0 For I = 1 to len1 Asc1 = ASC (mid (ascontents1, I, 1 )) If asc1 <0 then asc1 = 65536 + asc1 If asc1> 255 then K = K + 2 Else K = k + 1 End if Next Strunicodelen = K-1 End Function Function strunicode2ansi (ascontents) 'Convert a Unicode-encoded string to an ANSI-encoded string. Strunicode2ansi = "" Len1 = Len (ascontents) For I = 1 to len1 Varchar = mid (ascontents, I, 1) Varasc = ASC (varchar) If varasc <0 then varasc = varasc + 65536 If varasc> 255 then Varhex = hex (varasc) Varlow = left (varhex, 2) Varhigh = right (varhex, 2) Strunicode2ansi = strunicode2ansi & chrb ("& H" & varlow) & chrb ("& H" & varhigh) Else Strunicode2ansi = strunicode2ansi & chrb (varasc) End if Next End Function Function stransi2unicode (ascontents) 'Convert an ANSI-encoded string to a Unicode-encoded string. Stransi2unicode = "" Len1 = lenb (ascontents) If len1 = 0 Then exit function For I = 1 to len1 Varchar = midb (ascontents, I, 1) Varasc = ASCB (varchar) If varasc> 127 then Stransi2unicode = stransi2unicode & CHR (ASCW (midb (ascontents, I + 1, 1) & varchar )) I = I + 1 Else Stransi2unicode = stransi2unicode & CHR (varasc) End if Next End Function Function base64encode (ascontents) 'Encode the ANSI encoded string with base64 'Ascontents should be an ANSI encoded string (Binary strings can also be used) Dim lnposition Dim lsresult Dim char1 Dim char2 Dim char3 Dim char4 Dim byte1 Dim byte2 Dim byte3 Dim savebits1 Dim savebits2 Dim lsgroupbinary Dim lsgroup64 Dim M4, len1, len2 Len1 = lenb (ascontents) If len1 <1 then Base64encode = "" Exit Function End if M3 = len1 mod 3 If m3> 0 then ascontents = ascontents & string (3-m3, chrb (0 )) 'The complement BITs are used to facilitate computation. If m3> 0 then Len1 = len1 + (3-m3) Len2 = len1-3 Else Len2 = len1 End if Lsresult = "" For lnposition = 1 to len2 step 3 Lsgroup64 = "" Lsgroupbinary = midb (ascontents, lnposition, 3) Byte1 = ASCB (midb (lsgroupbinary, 1, 1): savebits1 = byte1 and 3 Byte2 = ASCB (midb (lsgroupbinary, 2, 1): savebits2 = byte2 and 15 Byte3 = ASCB (midb (lsgroupbinary, 3, 1 )) Char1 = midb (sbase_64_characters, (Bytes 1 and 252)/4) + 1, 1) Char2 = midb (sbase_64_characters, (byte2 and 240)/16) or (savebits1 * 16) and & HFF) + 1, 1) Char3 = midb (sbase_64_characters, (byte3 and 192)/64) or (savebits2 * 4) and & HFF) + 1, 1) Char4 = midb (sbase_64_characters, (byte3 and 63) + 1, 1) Lsgroup64 = char1 & char2 & char3 & char4 Lsresult = lsresult & lsgroup64 Next 'Process the remaining several characters If m3> 0 then Lsgroup64 = "" Lsgroupbinary = midb (ascontents, len2 + 1, 3) Byte1 = ASCB (midb (lsgroupbinary, 1, 1): savebits1 = byte1 and 3 Byte2 = ASCB (midb (lsgroupbinary, 2, 1): savebits2 = byte2 and 15 Byte3 = ASCB (midb (lsgroupbinary, 3, 1 )) Char1 = midb (sbase_64_characters, (Bytes 1 and 252)/4) + 1, 1) Char2 = midb (sbase_64_characters, (byte2 and 240)/16) or (savebits1 * 16) and & HFF) + 1, 1) Char3 = midb (sbase_64_characters, (byte3 and 192)/64) or (savebits2 * 4) and & HFF) + 1, 1) If m3 = 1 then Lsgroup64 = char1 & char2 & chrb (61) & chrb (61) 'Use = to fill the digits Else Lsgroup64 = char1 & char2 & char3 & chrb (61) 'Use = to fill in the digits End if Lsresult = lsresult & lsgroup64 End if Base64encode = lsresult End Function Function base64decode (ascontents) 'Convert a base64 encoded string to an ANSI encoded string 'Ascontents should also be an ANSI encoded string (Binary strings can also be used) Dim lsresult Dim lnposition Dim lsgroup64, lsgroupbinary Dim char1, char2, char3, char4 Dim byte1, byte2, byte3 Dim M4, len1, len2 Len1 = lenb (ascontents) M4 = len1 mod 4 If len1 <1 or M4> 0 then 'String length should be a multiple of 4 Base64decode = "" Exit Function End if 'Judge whether the last digit is equal to = 'Determine whether the second to last digit is equal to or not 'Here M4 indicates the number of remaining characters that need to be processed separately. If midb (ascontents, len1, 1) = chrb (61) Then M4 = 3 If midb (ascontents, len1-1, 1) = chrb (61) Then M4 = 2 If M4 = 0 then Len2 = len1 Else Len2 = len1-4 End if For lnposition = 1 to len2 Step 4 Lsgroupbinary = "" Lsgroup64 = midb (ascontents, lnposition, 4) Char1 = bytes B (sbase_64_characters, midb (lsgroup64, 1, 1)-1 Char2 = bytes B (sbase_64_characters, midb (lsgroup64, 2, 1)-1 Char3 = bytes B (sbase_64_characters, midb (lsgroup64, 3, 1)-1 Char4 = bytes B (sbase_64_characters, midb (lsgroup64, 4, 1)-1 Byte1 = chrb (char2 and 48)/16) or (char1 * 4) and & HFF) Byte2 = lsgroupbinary & chrb (char3 and 60)/4) or (char2 * 16) and & HFF) Byte3 = chrb (char3 and 3) * 64) and & HFF) or (char4 and 63 )) Lsgroupbinary = byte1 & byte2 & byte3 Lsresult = lsresult & lsgroupbinary Next 'Process the remaining several characters If M4> 0 then Lsgroupbinary = "" Lsgroup64 = midb (ascontents, len2 + 1, M4) & chrb (65) 'chr (65) = A, converted to 0 If M4 = 2 then' makes up four digits to facilitate computation Lsgroup64 = lsgroup64 & chrb (65) End if Char1 = bytes B (sbase_64_characters, midb (lsgroup64, 1, 1)-1 Char2 = bytes B (sbase_64_characters, midb (lsgroup64, 2, 1)-1 Char3 = bytes B (sbase_64_characters, midb (lsgroup64, 3, 1)-1 Char4 = bytes B (sbase_64_characters, midb (lsgroup64, 4, 1)-1 Byte1 = chrb (char2 and 48)/16) or (char1 * 4) and & HFF) Byte2 = lsgroupbinary & chrb (char3 and 60)/4) or (char2 * 16) and & HFF) Byte3 = chrb (char3 and 3) * 64) and & HFF) or (char4 and 63 )) If M4 = 2 then Lsgroupbinary = byte1 Elseif M4 = 3 then Lsgroupbinary = byte1 & byte2 End if Lsresult = lsresult & lsgroupbinary End if Base64decode = lsresult End Function |