News:

Function Finder  Find all the functions within source code files

Main Menu

Encode / Decode Base64 Strings

Started by kevin, May 22, 2013, 01:21:37 AM

Previous topic - Next topic

kevin

  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.
   

PlayBASIC Code: [Select]
   // 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$)
Login required to view complete source code



  Related Articles:

     * MD5 Hashing Library