Dynamic Flash

Confessions of a serial code abuser
  • rss
  • Home
  • MTASC
  • Archives
  • About me
  • Goodies
    • Base64 encoder/decoder class
  • My Bookshelf
  • My Talks

Base64 encoder/decoder class

Introduction

The Base64 class can encode String or ByteArray objects to Base 64 encoding and vice versa.

Download

Base64-1.1.0.zip

The project source is hosted at the as3base64 github repository

Adding as3base64 to your project

Before you can use the Base64 class, you need to add the as3base64.swc file to your library path.

If you’re using Flex Builder 3, simple copy the file into your libs directory and it’ll automatically be imported into your project.

If you’re using the Flex SDK command-line compiler you’ll need to add the SWC to your library path using the -library-path command-line switch:

mxmlc -library-path+=/path/to/as3base64.swc ...

Flash CS3 isn’t smart enough to know what to to with a SWC file, so if you’re using Flash CS3 you need to add the src directory to your classpath in your project’s Publish Settings.

If you’re one of the lucky people using Flash CS4, you just add the SWC to the classpath in your project’s Publish Settings.

Usage

Encoding from and decoding to String:

import com.dynamicflash.util.Base64;
 
var source:String = "Hello, world!";
var encoded:String = Base64.encode(source);
trace(encoded)
 
var decoded:String = Base64.decode(encoded);
trace(decoded);

Encoding from and decoding to a ByteArray:

import com.dynamicflash.util.Base64;
 
var obj:Object = {name:"Dynamic Flash", url:"http://dynamicflash.com"};
var source:ByteArray = new ByteArray();
source.writeObject(obj);
var encoded:String = Base64.encodeByteArray(source);
trace(encoded);
 
var decoded:ByteArray = Base64.decodeToByteArray(encoded);
var obj2:Object = decoded.readObject();
trace(obj2.name + "(" + obj2.url + ")");

Compiling the SWC

You need to make sure that the compc command-line compiler from the Flex SDK is on your path. Then run either build.sh (Mac/Linux) or build.bat (Windows) from a terminal window to rebuild the build/as3base64.swc file.

History

1.1.0

Repackaged library as a SWC so that you don’t have to have my code hanging around in your src folder.

1.0.0

Initial release.

Bug reports

Bug reports should be filed at http://dynamicflash.com/goodies/base64

License

The as3base64 is released under the MIT license.

Copyright (c) 2006 Steve Webster

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Comments rss
Comments rss
Trackback
Trackback

36 responses

[...] For more information see the Base64 class page, which

ActionScript 3.0 Base64 encoder/decoder at Dynamic Flash | Wednesday, 27 September 2006 | 8:51 pm

[...] For more information see the Base64 class page, which is part of the revamped Goodies section. [...]

[...] Base64 encoder/decoder class at Dynamic Flash (tags: Base64 as3)

danielyuen.hk Blog : links for 2006-09-29 | Friday, 29 September 2006 | 12:50 am

[...] Base64 encoder/decoder class at Dynamic Flash (tags: Base64 as3) [...]

Dude, you rock. This works great, and was exactly

Chris | Friday, 10 November 2006 | 1:07 am

Dude, you rock. This works great, and was exactly what I was looking for! In particular, the ability to encode a byte array.

[...] http://www.dynamicflash.com/goodies/base64 [...]

Actionscript Classes » Base64 | Saturday, 16 December 2006 | 5:08 am

[...] http://www.dynamicflash.com/goodies/base64 [...]

[...] Steve Webster’s Base64 class and just use the result

Iconara » Flex WebServices, problem with <any/> | Tuesday, 25 September 2007 | 8:05 am

[...] Steve Webster’s Base64 class and just use the result of Base64.encode(document.toXMLString()) as a parameter to the web service. [...]

[...] Base64 De- and Encoder in Actionscript [...]

Tinkerlog » Blog Archive » Webcam snapshots with Flex3 | Saturday, 03 November 2007 | 6:34 pm

[...] Base64 De- and Encoder in Actionscript [...]

Great stuff. If copying and pasting the examples above to

Emma Jones | Tuesday, 11 March 2008 | 12:24 am

Great stuff. If copying and pasting the examples above to test them - just watch the typo in line 1 of the first example about encoding and decoding from String; should be util not utils

Emma: Thanks for letting me know about the typo. I've

Steve | Wednesday, 12 March 2008 | 3:13 pm

Emma: Thanks for letting me know about the typo. I’ve just updated the code example :)

excellent stuff! This does exactly what i needed! Kudo's for you!

erik | Monday, 31 March 2008 | 6:34 pm

excellent stuff! This does exactly what i needed! Kudo’s for you!

very useful.. thanks!

backz | Tuesday, 13 May 2008 | 4:18 pm

very useful.. thanks!

I was kind of hoping there'd be one in as3corelib;

Karel | Thursday, 15 May 2008 | 7:09 am

I was kind of hoping there’d be one in as3corelib; but there isn’t. Thank you for sharing this

[...] is only available to Flex. Luckily with the richness

import mx.utils.Base64Decoder in Flash CS3 | import Slice.of.Infinity; | Wednesday, 16 July 2008 | 7:06 am

[...] is only available to Flex. Luckily with the richness of the web, I found an alternative dynamicflash’s Base 64 Class . However, i still think it’s possible to use Flex library into Flash (AS3) and i found out [...]

[...] http://dynamicflash.com/goodies/base64/ « b+????? [...]

?? » Blog Archive » Base64 encoder/decoder class | Sunday, 20 July 2008 | 7:43 am

[...] http://dynamicflash.com/goodies/base64/ « b+????? [...]

thanx.

sses | Monday, 28 July 2008 | 11:18 pm

thanx.

[...] http://dynamicflash.com/goodies/base64/ ???? [...]

Flash?amfphp??ByteArray???? | ? ? ? ? | Thursday, 21 August 2008 | 9:51 am

[...] http://dynamicflash.com/goodies/base64/ ???? [...]

[...] Base64 encoder/decoder class This class

Save images from Flash to your server using ASP.NET webservices : Ryan Bosinger | Thursday, 18 September 2008 | 9:26 pm

[...] Base64 encoder/decoder class This class from Dynamic Flash will be used to encode our ByteArray to Base64. [...]

Thanks for the information. please keep posting such useful info.

Encoder/Decoder | Monday, 22 September 2008 | 12:08 pm

Thanks for the information. please keep posting such useful info.

[...] ByteArray??????????????? Adobe? corelib? dynamicflash.com? com.dynamicflash.util.Base64? [...]

AS3, ??????????????(JPG,PNG) | ????tv?? | Wednesday, 08 October 2008 | 7:35 pm

[...] ByteArray??????????????? Adobe? corelib? dynamicflash.com? com.dynamicflash.util.Base64? [...]

Very Useful.. Thanks !!!

JHKim | Tuesday, 11 November 2008 | 2:19 am

Very Useful.. Thanks !!!

Hello, Thanks for sharing this with the community, there is a

Neil | Wednesday, 03 December 2008 | 4:15 pm

Hello,

Thanks for sharing this with the community, there is a typo in the first example;

var decoded:String = Base64.decode(source); trace(decoded);

Should say;

var decoded:String = Base64.decode(encoded); trace(decoded);

Neil: Thanks for that - I've updated the example to

Steve | Wednesday, 03 December 2008 | 4:55 pm

Neil: Thanks for that - I’ve updated the example to fix the typo.

If found a small defect in the code as it

Kevc | Monday, 29 December 2008 | 7:28 pm

If found a small defect in the code as it stands…Fix included

According to the ITEF the decoder MUST ignore all characters not included in the enocde set.

Change the following lines in the decode routine: // While there are data bytes left to be processed for (var i:uint = 0; i < data.length; i += 4) { // Populate data buffer with position of Base64 characters for // next 4 bytes from encoded data for (var j:uint = 0; j < 4 && i + j < data.length; j++) { dataBuffer[j] = BASE64_CHARS.indexOf(data.charAt(i + j)); }

To this:

// While there are data bytes left to be processed for (var i:uint = 0; i < data.length; i += 4) { // Populate data buffer with position of Base64 characters for // next 4 bytes from encoded data and throw away the non-encoded characters. for (var j:uint = 0; j < 4 && i + j < data.length; j++) { dataBuffer[j] = BASE64_CHARS.indexOf(data.charAt(i + j)); while((dataBuffer[j] < 0) && (i < data.length)) { i++; dataBuffer[j] = BASE64_CHARS.indexOf(data.charAt(i + j)); } }

Thanks for the code it help out a lot!

Kevin Cheung

thankx

yellow | Thursday, 08 January 2009 | 2:51 am

thankx

[...] Oh, and I’m using Steve Websters base64 class for

Bombwar » This weekends update | Monday, 12 January 2009 | 5:08 am

[...] Oh, and I’m using Steve Websters base64 class for the map data I was talking about in the last post. and can be downloaded here http://dynamicflash.com/goodies/base64/ [...]

Just wanted to let you know that I'm using your

Kevin Hoyt | Friday, 16 January 2009 | 5:34 pm

Just wanted to let you know that I’m using your Base64 class in a set of JavaScript-based AIR samples. Sounds strange, I know, but the pure JavaScript versions out there were incomplete and/or hard to use. You kept it simple with static methods.

Thanks for your work and for the MIT license, Kevin Hoyt Platform Evangelist Adobe Systems, Inc.

THANK YOU SO MUCH! This made my application work. There

Tim K | Thursday, 22 January 2009 | 4:57 pm

THANK YOU SO MUCH! This made my application work. There was some sort of bug going on with uncompress() method of ByteArray. But by encoding my compressed byte array before I stored my variable into the database, I was able to successfully uncompress it after I decoded it. Oh yeah I also had to escape and unescape the damn thing. Talk about confusing! But it worked thank you!

Oh I also wanted to tell you how much I

Tim K | Thursday, 22 January 2009 | 5:06 pm

Oh I also wanted to tell you how much I was able to trim by combining compress and Base64 encode. Note: I am working on an LMS called CourseMill that URLEncodes everything, turned out to be in my favor.

Originally, I was just passing an XMLString, which was roughly 65000 characters long after it was URLEncoded. Then I tried compress(), which reduced it to a very small amount of characters, probably under 1000. But when I stored the compress var onto the LMS it was URLEncoded and shot up to around 33000 characters. The shining star here is the Base64 encoding which is 99.9% safe from URL Encoding. So after compressing AND encoding with Base64, my var was reduced to a 13000!!!

VERY COOL!!!

Thank you very much, this works very well to replace

James Corbett | Wednesday, 11 February 2009 | 6:35 pm

Thank you very much, this works very well to replace buggy AS3crypto command.

cheers for the base64 work. I needed to do some

baz | Wednesday, 08 April 2009 | 5:02 pm

cheers for the base64 work. I needed to do some crazy flash data sending over localconnection, and it works a treat. (watching out for those sneaky localconnection size limits though!).

[...] Base64 encoder/decoder class8,9 [...]

Edit Profile « LogField | Thursday, 16 April 2009 | 5:02 am

[...] Base64 encoder/decoder class8,9 [...]

Wow! This really helped me out, great THANKS! I needed

Jessica | Wednesday, 22 April 2009 | 8:17 am

Wow! This really helped me out, great THANKS! I needed to decode something from base64 and your class is really clear and simple.

Thanks man!

[...] Next we encode the resulting ByteArrays with the Base64

Inside the Classic | publicreative lab | Thursday, 30 April 2009 | 4:19 pm

[...] Next we encode the resulting ByteArrays with the Base64 encoder. You can grab this from Dynamic Flash. [...]

Hi I couldnt get this to work. Flex builder keeps giving

Michael | Monday, 01 June 2009 | 11:32 am

Hi

I couldnt get this to work. Flex builder keeps giving me 1172: Definition com.dynamicflash2.util:Base64 could not be found.

But its there, I have tried both using the as file and the swc file.

So strange.

Hi Can I use this without using Flex? I am using

Caezar | Friday, 05 June 2009 | 3:06 am

Hi

Can I use this without using Flex? I am using Flash CS3

hi, thanks for your work! anyway i've a problem: i want

elia | Friday, 05 June 2009 | 12:56 pm

hi, thanks for your work! anyway i’ve a problem: i want to encode (ok!) and then decode (not ok) an image and then visualize it. what must i do? how can i send my code? thanks

gr8 work maaan.....actually we can encode byteArray with help base64Encoder

Mitesh dave | Thursday, 18 June 2009 | 2:45 pm

gr8 work maaan…..actually we can encode byteArray with help base64Encoder ans base64Decoder class….of flex…but the problem is the, i was using flex 3.0.0 version ,which gives problem on MAC for encoding……thanks for posting your work……u made my day….

Leave a comment

You can use these tags : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="">

About Dynamic Flash

Steve Webster is a Senior Web Developer for Yahoo! in London, UK.

He is more than a little concerned that he defines himself in terms of his career, and that he talks about himself in the third person.

Find out more

Sponsored Links

del.icio.us-ed

  • camen design ยท Video for Everybody!
  • Beyond Flash, an addendum
  • IE8 Security Part VII: ClickJacking Defenses
  • Security/CSP/Spec - MozillaWiki
  • RED: Resource Expert Droid
  • Notes on accessibility of text replacement using HTML5 canvas
  • Compatibility View and "Smart Defaults"
  • Cocos2D and Box2D on iPhone
  • JavaScript InfoVis Toolkit
  • js-test-driver

Recent Posts

  • iPhone / iPod Touch Development Resources
  • Upgrading your app to AIR 1.5
  • Motivate yourself by doing it in public
  • The trouble with Flash and REST
  • And we’re back

Tags

Accessibility ActionScript actionscript3 actionscript 3.0 air apple astro book calendar conference designer filereference file upload Flash flex fotb08 framework getters head head08 head2008 headconference html5 http ical internet internet explorer JavaScript jobs junior microsoft opportunities representational state transfer rest ria schedule silverlight singularity08 skin tutorial urlrequest web developer web development Web Standards Yahoo!

Stuff

Singularity?
Flex.org - The Directory for Flex
rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox