Base64 Encoding Class in ActionScript 3

Written by Garry Lachman (Admin). Filed under ActionScript 3. Tagged , , , , , , , , , , , . Bookmark the Permalink. Post a Comment. Leave a Trackback URL.

This is a class for Base64 Encoding by Steve Webster…
Very Usefull one!!!

Base64.as

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package {
/*
Base64 - 1.1.0

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.
*/


package {

        import flash.utils.ByteArray;
       
        public class Base64 {
               
                private static const BASE64_CHARS:String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

                public static const version:String = "1.1.0";

                public static function encode(data:String):String {
                        // Convert string to ByteArray
                        var bytes:ByteArray = new ByteArray();
                        bytes.writeUTFBytes(data);
                       
                        // Return encoded ByteArray
                        return encodeByteArray(bytes);
                }
               
                public static function encodeByteArray(data:ByteArray):String {
                        // Initialise output
                        var output:String = "";
                       
                        // Create data and output buffers
                        var dataBuffer:Array;
                        var outputBuffer:Array = new Array(4);
                       
                        // Rewind ByteArray
                        data.position = 0;
                       
                        // while there are still bytes to be processed
                        while (data.bytesAvailable > 0) {
                                // Create new data buffer and populate next 3 bytes from data
                                dataBuffer = new Array();
                                for (var i:uint = 0; i < 3 && data.bytesAvailable > 0; i++) {
                                        dataBuffer[i] = data.readUnsignedByte();
                                }
                               
                                // Convert to data buffer Base64 character positions and
                                // store in output buffer
                                outputBuffer[0] = (dataBuffer[0] & 0xfc) >> 2;
                                outputBuffer[1] = ((dataBuffer[0] & 0x03) << 4) | ((dataBuffer[1]) >> 4);
                                outputBuffer[2] = ((dataBuffer[1] & 0x0f) << 2) | ((dataBuffer[2]) >> 6);
                                outputBuffer[3] = dataBuffer[2] & 0x3f;
                               
                                // If data buffer was short (i.e not 3 characters) then set
                                // end character indexes in data buffer to index of '=' symbol.
                                // This is necessary because Base64 data is always a multiple of
                                // 4 bytes and is basses with '=' symbols.
                                for (var j:uint = dataBuffer.length; j < 3; j++) {
                                        outputBuffer[j + 1] = 64;
                                }
                               
                                // Loop through output buffer and add Base64 characters to
                                // encoded data string for each character.
                                for (var k:uint = 0; k < outputBuffer.length; k++) {
                                        output += BASE64_CHARS.charAt(outputBuffer[k]);
                                }
                        }
                       
                        // Return encoded data
                        return output;
                }
               
                public static function decode(data:String):String {
                        // Decode data to ByteArray
                        var bytes:ByteArray = decodeToByteArray(data);
                       
                        // Convert to string and return
                        return bytes.readUTFBytes(bytes.length);
                }
               
                public static function decodeToByteArray(data:String):ByteArray {
                        // Initialise output ByteArray for decoded data
                        var output:ByteArray = new ByteArray();
                       
                        // Create data and output buffers
                        var dataBuffer:Array = new Array(4);
                        var outputBuffer:Array = new Array(3);

                        // 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));
                                }
                       
                        // Decode data buffer back into bytes
                                outputBuffer[0] = (dataBuffer[0] << 2) + ((dataBuffer[1] & 0x30) >> 4);
                                outputBuffer[1] = ((dataBuffer[1] & 0x0f) << 4) + ((dataBuffer[2] & 0x3c) >> 2);                
                                outputBuffer[2] = ((dataBuffer[2] & 0x03) << 6) + dataBuffer[3];
                               
                                // Add all non-padded bytes in output buffer to decoded data
                                for (var k:uint = 0; k < outputBuffer.length; k++) {
                                        if (dataBuffer[k+1] == 64) break;
                                        output.writeByte(outputBuffer[k]);
                                }
                        }
                       
                        // Rewind decoded data ByteArray
                        output.position = 0;
                       
                        // Return decoded data
                        return output;
                }
               
                public function Base64() {
                        throw new Error("Base64 class is static container only");
                }
        }
}

Useage:

1
2
3
4
5
var aString:String = "to Convert";
trace(Base64.encode(aString)); // Base64 String

var aBase64:String = "dG8gQ29udmVydA==";
trace(Base64.decode(aBase64)) // Original String

Have Fun ;)

Share

7 Comments

  1. Marcus
    Posted October 13, 2010 at 11:49 am | Permalink

    Thanks for sharing this good class; made my life much easier :)

  2. cool-man
    Posted November 2, 2010 at 1:48 pm | Permalink

    can this class encod image or file ?

  3. admin
    Posted December 8, 2010 at 1:28 pm | Permalink

    Yes cool-man… but first u need to convert the image to ByteArray….
    check out this post…
    http://marstonstudio.com/2007/10/19/how-to-take-a-snapshot-of-a-flash-movie-and-automatically-upload-the-jpg-to-a-server-in-three-easy-steps/

    You can encode to Base64 the PNG ByteArray….

  4. K Manny
    Posted April 8, 2011 at 1:31 am | Permalink

    Thanks alot! Great Work

  5. Posted April 19, 2012 at 12:10 pm | Permalink

    Thanks for sharing such a nice post…….

  6. Posted August 9, 2012 at 12:48 pm | Permalink

    thanks for this, comes very handy in my game’s level editor!

  7. Posted March 3, 2013 at 5:53 pm | Permalink

    wonderful issues altogether, you just gained a new reader.

    What could you recommend about your post that you simply made a few
    days in the past? Any positive?

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*