Function B64Decode(strText) 'Base 64 decoding takes a stream of input characters, each 'of which represents six bits, and reassembles a new stream 'comprised of eight bit words. The easiest way to decode it 'is to convert blocks of four 6-bit input words into blocks 'of three 8-bit output words. ' |ŻŻŻŻ||ŻŻŻŻ||ŻŻŻŻ||ŻŻŻŻ| ' 111111000000111111000000 ' |______||______||______| 'Here are the input characters which represent the 6-bit words: Const B64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" 'An "A" represents "000000", "B" is "000001", and a "/" represents "111111" 'Because we wil be processing four input words at a time, we must be sure 'our input is a multiple of four characters long. To make the length count 'accurate, we will first remove anything other than the valid 64 characters 'mentioned above. Dim strIn, strOut, strBuffer, strChar Dim lngLength, lngCount Dim intIn1, intIn2, intIn3, intIn4 Dim intOut1, intOut2, intOut3 Dim strOut1, strOut2, strOut3 strIn = strText 'Don't modify the input (I don't like to use ByVal) 'Remove all illegal characters - easier than ignoring them later lngLength = Len(strIn) For lngCount = 1 To lngLength strChar = Mid(strIn, lngCount, 1) If InStr(B64, strChar) <> 0 Then strBuffer = strBuffer & strChar End If Next strIn = strBuffer strBuffer = "" 'Pad to make it a multiple of 4 characters long lngLength = Len(strIn) Do While Len(strIn) Mod 4 <> 0 strIn = strIn & "=" Loop 'Process blocks of four input characters For lngCount = 1 To lngLength Step 4 'Get the four six-bit values intIn1 = InStr(B64, Mid(strIn, lngCount, 1)) - 1 intIn2 = InStr(B64, Mid(strIn, lngCount + 1, 1)) - 1 intIn3 = InStr(B64, Mid(strIn, lngCount + 2, 1)) - 1 intIn4 = InStr(B64, Mid(strIn, lngCount + 3, 1)) - 1 'As we calculate each value, we have to constantly check for the 'input value of -1, which indicates we hit a pad characters at the end. 'The first output is the first input and upper two bits of second input intOut1 = intIn1 * 4 'the entire first input If intIn2 <> -1 Then intOut1 = intOut1 + (intIn2 \ 16) 'the upper two bits of the second input End If strOut1 = Chr(intOut1) 'The second output is the lower 4 bits of the second input and upper 4 bits of the third If intIn2 <> -1 Then intOut2 = (intIn2 And 15) * 16 'lower 4 bits of the second input If intIn3 <> -1 Then intOut2 = intOut2 + (intIn3 \ 4) 'upper 4 bits of the third input End If strOut2 = Chr(intOut2) Else strOut2 = "" End If 'The third output is the lower two bits of the third input and all the fourth If intIn3 <> -1 Then intOut3 = (intIn3 And 3) * 64 If intIn4 <> -1 Then intOut3 = intOut3 + intIn4 'all the fourth End If strOut3 = Chr(intOut3) Else strOut3 = "" End If 'Append the three characters to the output strOut = strOut & strOut1 & strOut2 & strOut3 Next 'Return the output B64Decode = strOut End Function Function B64Encode(strText) 'Base 64 encoding takes an input stream and considers 6 bits 'at a time. One of 64 pre-selected characters is chosen to represent 'that block of six bits. Here is the list of 64 output characters: Const B64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" 'The easy way of encoding is to take three 8-bit characters and 'convert them into four 6-bit characters. ' |ŻŻŻŻŻŻ||ŻŻŻŻŻŻ||ŻŻŻŻŻŻ| ' 111111110000000011111111 ' |____||____||____||____| 'Since the easy way needs groups of three input characters at 'a time, we may have to add dummy "pad" characters on the end. 'However, we don't want to actually include the added pad 'characters in the output! Since we will only add two (at most) 'pad characters, only the third and fourth output characters 'are in danger of relying strictly on input pad characters. If 'we determine a pad is being processed, we stop. Dim strIn, strOut Dim lngCount, lngLength Dim intIn1, intIn2, intIn3 Dim intOut1, intOut2, intOut3, intOut4 Dim strOut1, strOut2, strOut3, strOut4 strIn = strText 'Preserve the input. I could have used ByVal... 'Make the input a multiple of 3 characters in length by adding pads. lngLength = Len(strIn) Do Until Len(strIn) Mod 3 = 0 strIn = strIn & Chr(0) Loop For lngCount = 1 To lngLength Step 3 'Get the three input characters byte values intIn1 = Asc(Mid(strIn, lngCount, 1)) intIn2 = Asc(Mid(strIn, lngCount + 1, 1)) intIn3 = Asc(Mid(strIn, lngCount + 2, 1)) 'The first output is the upper six bits of the first input intOut1 = intIn1 \ 4 'divide to throw away the lower two bits strOut1 = Mid(B64, intOut1 + 1, 1) 'The second output is the lower 2 bits of the first input 'and the upper four bits of the second input intOut2 = (intIn1 And 3) * 16 'the lower two bits of the first input intOut2 = intOut2 + (intIn2 \ 16) 'the upper four bits of the second input strOut2 = Mid(B64, intOut2 + 1, 1) 'Are we looking at an end pad byte? If lngCount + 1 > lngLength Then 'Ignore input pad bytes and output nothing strOut3 = "" Else 'The third output is the lower four of the second input 'and the upper two bits of the third input intOut3 = (intIn2 And 15) * 4 'the lower four bits of the second input intOut3 = intOut3 + (intIn3 \ 64) 'the upper two bits of the third input strOut3 = Mid(B64, intOut3 + 1, 1) End If 'Are we looking at an end pad byte? If lngCount + 2 > lngLength Then 'Ignore input pad bytes and output nothing strOut4 = "" Else 'The fourth output is the lower six bits of the third input intOut4 = (intIn3 And 63) strOut4 = Mid(B64, intOut4 + 1, 1) End If 'Append the four characters to the output strOut = strOut & strOut1 & strOut2 & strOut3 & strOut4 Next 'Pad the output length to make it easy for the decoder Do Until Len(strOut) Mod 4 = 0 strOut = strOut & "=" Loop 'Break into 64 character lines If Len(strOut) > 64 Then strIn = strOut strOut = "" For lngCount = 1 To Len(strIn) Step 64 If strOut <> "" Then strOut = strOut & vbCrLf strOut = strOut & Mid(strIn, lngCount, 64) Next End If B64Encode = strOut End Function