ASP: Base64 encoding, decoding function www.xufei.net [2003-3-6]
Base64 encoding, decoding function This is what I read a few Base64 encodings, and I rewrite it after decoding the function.
Because, in the VBScript of the Chinese operating system, the Unicode character set is used, so many base64 encoders, the decoding function is theoretically correct, but actually can't run! File name base64test.asp <% sBASE_64_CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 /" sBASE_64_CHARACTERS = strUnicode2Ansi (sBASE_64_CHARACTERS) Function strUnicodeLen (asContents) 'Ansi encoded unicode string length is calculated asContents1 = "a" & asContentslen1 = len (asContents1) k = 0for i = 1 to len1asc1 = asc (mid (asContents1, i, 1)) if asc1 <0 then asc1 = 65536 asc1if asc1> 255 thenk = k 2elsek = k 1end ifnextstrUnicodeLen = k-1End Function Function strUnicode2Ansi (asContents) 'will Unicode encoded string, converted to ANSI encoded string strunicode2ansi = "" len1 = len (ascontents) for i = 1 to len1varchar = MID (Ascontents, I, 1) Varasc = ASC (varchar) if Varasc <0 Then Varasc = varasc 65536if varasc> 255 thenvarHex = Hex (varasc) varlow = left (varHex, 2) varhigh = right (varHex, 2) strUnicode2Ansi = strUnicode2Ansi & chrb ( "& H" & varlow) & chrb ( "& H" & varhigh) elsestrUnicode2Ansi = strUnicode2Ansi & chrb (varasc) end ifnextEnd function function strAnsi2Unicode (asContents) 'the Ansi encoded string, is converted into a Unicode string strAnsi2Unicode = "" len1 = lenb (asContents) if len1 = 0 then exit functionfor i = 1 to Le n1varchar = midb (asContents, i, 1) varasc = ascb (varchar) if varasc> 127 then strAnsi2Unicode = strAnsi2Unicode & chr (ascw (midb (asContents, i 1,1) & varchar)) i = i 1elsestrAnsi2Unicode = strAnsi2Unicode & chr (var (var (var (var (var (var (var (varic) end ifnextend function function base64encode (ascontents) 'performs ANSI encoded strings Base64 encoded' Ascontents should be ANSI encoded strings (binary strings can also be)
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 Functionend if M3 = len1 mod 3 if m3> 0 THEN Ascontents = Ascontents & String (3-m3, chrb (0)) 'Supplement bits are for easy calculation of IF M3> 0 THEN LEN1 = LEN1 (3-m3) len2 = len1- 3elselen2 = len1end 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, ((Byte1 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) LSGRO up64 = Char1 & Char2 & Char3 & Char4 lsResult = lsResult & lsGroup64 Next 'processes the last few remaining characters if M3> 0 thenlsGroup64 = "" 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, ((BYTE1 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 ThenlsGroup64 = Char1 & Char2 & Chrb (61) & chrb (61) 'Use = complement number digits elselsGroup64 = char1 & char2 & Char3 & ChrB (61) '= a number of bits make up the end if lsResult = lsResult & lsGroup64 end if Base64encode = lsResult End Function Function Base64decode (asContents)' Base64 encoded string into the ANSI encoded string 'ascontents should also be ANSI encoded strings (binary strings can also be) DIM LSRESULT DIM LNPSITION DIM LSGROUP64, LSGROUPBINARY DIM CHAR1, CHAR2, CHAR3, CHAR4 DIM BYTE11, BYTE2, BYTE3 DIM M4, LEN1, LEN2 LEN1 = lenb (ascontents) m4 = len1 mod 4 if len1 <1 or M4> 0 THEN 'string length should be 4 multiples 64decode = "" EXIT function end if' judgment the last bit is = 号 'judgment countdown Two is not = 号 'This M4 indicates the last remaining character number IF MIDB (Ascontents, Len1, 1) = ChRB (61) THEN M4 = 3 if MIDB (Ascontents, Len1-1, 1) = chrb (61) then m4 = 2 if m4 = 0 thenlen2 = len1elselen2 = len1-4end if For lnPosition = 1 To len2 Step 4 lsGroupBinary = "" lsGroup64 = Midb (asContents, lnPosition, 4) char1 = InStrb (sBASE_64_CHARACTERS, Midb ( Lsgroup64, 1, 1)) - 1 Char2 = INSTRB (SBASE_64_Characters, MIDB (Lsgroup64, 2, 1)) - 1 Char3 = INSTRB (SBase_64_Characters, MIDB (Lsgroup64, 3, 1)) - 1 Char4 = INSTRB (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 '