UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: kevin on May 22, 2013, 01:21:37 AM

Title: Encode / Decode Base64 Strings
Post by: kevin on May 22, 2013, 01:21:37 AM
  Encode / Decode Base64 Strings

  These examples includes functions to encode/decode string containing BASE64 data (6bit) back into an 8bit stream.  The functions are binary safe.
   

[pbcode]



   // Original decode Test
   s$ = "VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw=="
   s$+= "VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw=="

   print "Decode Test"
   print s$
   print DecodeBase64(S$)
   print ""

   // Encode / Decode Test
   print "Encode / Decode Test"

   Message$="This is an encoded string"
   result$=EncodeBase64(Message$)
   print result$
   print  DecodeBase64(Result$)   
   print ""

   
   Sync
   waitkey
   


   ; --------------------------------------------------------------------------
   ; --------------------------------------------------------------------------
   ; ---Decode BASE 64 Encoded String-----------------------------------------
   ; --------------------------------------------------------------------------
   ; --------------------------------------------------------------------------

Function DecodeBase64(S$)

   local lp,Stream$

   // Build Decode table for
   dim Base64LevelChrTable(255)
   for lp=asc("a") to asc("z")
      Base64LevelChrTable(lp)=true
      Base64LevelChrTable( (lp-asc("a"))+asc("A"))=true
   next   

   for lp=asc("0") to asc("9")
      Base64LevelChrTable(lp)=true
   next   

   Base64LevelChrTable(asc("+"))=true
   Base64LevelChrTable(asc("/"))=true
   Base64LevelChrTable(asc("="))=true
   

   // strip input string of any illegal characters

   Stream$=""
   
   For lp=1 to Len(s$)
      ThisChr=mid(s$,lp)
      if Base64LevelChrTable(ThisChr)
            Stream$+=chr$(ThisChr)
      endif      
   next

   if Len(Stream$)

      ; ------------------------------------------------------------------
      // Build the BASE64 decoder Table For this fragment
      ; ------------------------------------------------------------------
      dim Base64DecodeTable(255)
      
      for lp=asc("A") to asc("Z")
            Base64DecodeTable(lp)=lp-asc("A")
      next
      
      for lp=asc("a") to asc("z")
            Base64DecodeTable(lp)=lp-asc("a")+ 26
      next

      for lp=asc("0") to asc("9")
            Base64DecodeTable(lp)=lp-asc("0")+ 52
      next

      Base64DecodeTable(asc("+"))=62
      Base64DecodeTable(asc("/"))=63
      
      
      local StreamSize=Len(Stream$)
      local StreamSize2   =StreamSize-4

      ; ------------------------------------------------------------------
      For lp=1 to StreamSize step 4
      ; ------------------------------------------------------------------
            
            ThisChr1 =mid(s$,lp)

            if lp<=StreamSize2
               ThisChr2 =mid(s$,lp+1)
               ThisChr3 =mid(s$,lp+2)
               ThisChr4 =mid(s$,lp+3)
            else
               ThisChr2 =asc("A")
               ThisChr3 =asc("A")
               ThisChr4 =asc("A")
               if lp+1<=StreamSize then ThisChr2=mid(s$,lp+1)
               if lp+2<=StreamSize then ThisChr3=mid(s$,lp+2)
               if lp+3<=StreamSize then ThisChr4=mid(s$,lp+3)
            endif

            // Convert the ASC characters to 6 bit values (0 to 63)
            Byte1=Base64DecodeTable(ThisChr1)
            Byte2=Base64DecodeTable(ThisChr2)
            Byte3=Base64DecodeTable(ThisChr3)
            Byte4=Base64DecodeTable(ThisChr4)

            // Dump to the output stream
            Result$+= chr$( (Byte1*4) |  (Byte2/16))

            // check for 'end' tokens            
            if ThisChr2<> asc("=")
                  TempChr =(Byte2 & 15)*16
                  TempChr+=(Byte3/4)
                  Result$+= chr$(TempChr)
            endif

            if ThisChr3<> asc("=")
                  TempChr =(Byte3 & $3)*64
                  TempChr+=Byte4
                  Result$+= chr$(TempChr)
            endif

      next

   endif


EndFunction Result$



   ; --------------------------------------------------------------------------
   ; --------------------------------------------------------------------------
   ; ---Encode BASE 64 Encoded String-----------------------------------------
   ; --------------------------------------------------------------------------
   ; --------------------------------------------------------------------------

Function EncodeBase64(s$)

   local StreamSize=Len(s$)
   Local StreamSize2=StreamSize-3      
   local Index
   if StreamSize
   
      ; ------------------------------------------------------------------
      ; Build the Encode Table For this fragment
      ; ------------------------------------------------------------------
      dim Base64EncodeTable$(255)
      
      for lp=asc("A") to asc("Z")
            Base64EncodeTable$(Index)=chr$(lp)   ;-asc("A")
            Index++
      next
      
      for lp=asc("a") to asc("z")
            Base64EncodeTable$(index)=chr$(lp)   ;-asc("a")+ 26
            Index++
      next

      for lp=asc("0") to asc("9")
            Base64EncodeTable$(index)=chr$(lp)
            index++
      next

      Base64EncodeTable$(62)="+"
      Base64EncodeTable$(63)="/"
      

      ; ------------------------------------------------------------------
      For lp=1 to StreamSize step 3
      ; ------------------------------------------------------------------

            Byte1 =mid(s$,lp)

            if lp<=StreamSize2
                  Byte2 =mid(s$,lp+1)
                  Byte3 =mid(s$,lp+2)
            else
                  Byte2=0
                  Byte3=0
                  if (lp+1)<=StreamSize then Byte2 =mid(s$,lp+1)
                  if (lp+2)<=StreamSize then Byte3 =mid(s$,lp+2)
            endif

            ThisChr1    = Byte1 / 4      // Move top 6bits down into bottom 6bits
            ThisChr2    = (Byte1 & 3) *16
            ThisChr2   +=   (Byte2 / 16)      
            ThisChr3  = (Byte2 & $f) *4
            ThisChr3 += (Byte3 / 64)
            ThisChr4  = (Byte3 & $3f)

            result$+=Base64EncodeTable$(ThisChr1)
            result$+=Base64EncodeTable$(ThisChr2)
            
            if (lp+1)<=StreamSize
               result$+=Base64EncodeTable$(ThisChr3)
            else
               result$+="="
            endif
            if (lp+2)<=StreamSize
               result$+=Base64EncodeTable$(ThisChr4)
            else
               result$+="="
            endif
      next

   endif
EndFunction result$

[/pbcode]


  Related Articles:

     * MD5 Hashing Library (http://www.underwaredesign.com/forums/index.php?topic=3517.0)