Encode decode in Flex using Base64


I am developing a flex AIR application in which I will store some data in a text file on user’s local machine. But the data is somehow private and confidential so I thought I should encrypt that data. I searched a bit how to encrypt the data and my search result made me happy, I come to know that Flex has already a capability to encrypt and decrypt the string using base64 encoding. Its wonderful!

First you will have to import to classes

import mx.utils.Base64Encoder;
import mx.utils.Base64Decoder;

Now first create a encoder object

private var myEncoder:Base64Encoder = new Base64Encoder();

Call the encode method

myEncoder.encode("I'm being encoded"); // It will encode the characters of  string in Base64 and add the result to an internal buffer.

// now to access the encoded string use this
var encodedText:String = myEncoder.toString();

In encodedText variale you will have the encoded string, yes and that text I saved to my text file.

Now to read again the original text we’ll will have to decode the encoded string, so what you have to do is

First create the decoder object

private var myDecoder:Base64Decoder = new Base64Decoder();

Call the decode method


here we just pass the Base64 encoded string as parameter for decode method, again the result will be stored in an internal buffer.

Now to get the original string first we’ll have to get the encoded string in ByteArray and than we will convert it to string like in this way

var decodedByteArr:ByteArray = myDecoder.toByteArray();

var decodedText = decodedByteArr.toString();

That’s it.

Now lets say there is a image which is base64 encoded, so how we can convert the encoded string to image. For that use this

img.load(decodedByteArr); // in the load method pass the ByteArray

<mx:Image id=”img” />

That’s it, as usual so simple.




8 thoughts on “Encode decode in Flex using Base64

    • sick bro….im tryna use that base64encoder object right now to parse the (<) angle bracket. sigh lol! btw is there a link to ur client?

  1. Hi

    I have captured a signature on the Flex side but want to send the signature via HTTPservice. I’m encoding the signature using

    public function encodedSignature():String
    var snapshot:ImageSnapshot = ImageSnapshot.captureImage(signature, 0, new JPEGEncoder());

    return ImageSnapshot.encodeImageAsBase64( snapshot )

    And then inserting into XML , but I’m getting CRLF every 76 charcters.

    I tried var imageSaveXML:XML= new XML(“”+
    encodedSignature().replace(/\r\n/gm, “”) + “”);

    But I’m still getting my encodedSignature spread over multiple lines instead of a single line

  2. Good article. But encrypting is something different to encoding. When you encrypt a message only the one who has private key can decrypt it so it’s secure. When you encode it can be decoded by anyone who has at least some IT background and can guess encoding algorithm (and base64 is very distinguishable).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s