Skip to content

Instantly share code, notes, and snippets.

@stubbetje
Created November 9, 2009 14:34
Show Gist options
  • Save stubbetje/229984 to your computer and use it in GitHub Desktop.
Save stubbetje/229984 to your computer and use it in GitHub Desktop.
Base64 encode and decode in javascript
var Base64 = {
characters: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=" ,
encode: function( string )
{
var characters = Base64.characters;
var result = '';
var i = 0;
do {
var a = string.charCodeAt(i++);
var b = string.charCodeAt(i++);
var c = string.charCodeAt(i++);
a = a ? a : 0;
b = b ? b : 0;
c = c ? c : 0;
var b1 = ( a >> 2 ) & 0x3F;
var b2 = ( ( a & 0x3 ) << 4 ) | ( ( b >> 4 ) & 0xF );
var b3 = ( ( b & 0xF ) << 2 ) | ( ( c >> 6 ) & 0x3 );
var b4 = c & 0x3F;
if( ! b ) {
b3 = b4 = 64;
} else if( ! c ) {
b4 = 64;
}
result += Base64.characters.charAt( b1 ) + Base64.characters.charAt( b2 ) + Base64.characters.charAt( b3 ) + Base64.characters.charAt( b4 );
} while ( i < string.length );
return result;
} ,
decode: function( string )
{
var characters = Base64.characters;
var result = '';
var i = 0;
do {
var b1 = Base64.characters.indexOf( string.charAt(i++) );
var b2 = Base64.characters.indexOf( string.charAt(i++) );
var b3 = Base64.characters.indexOf( string.charAt(i++) );
var b4 = Base64.characters.indexOf( string.charAt(i++) );
var a = ( ( b1 & 0x3F ) << 2 ) | ( ( b2 >> 4 ) & 0x3 );
var b = ( ( b2 & 0xF ) << 4 ) | ( ( b3 >> 2 ) & 0xF );
var c = ( ( b3 & 0x3 ) << 6 ) | ( b4 & 0x3F );
result += String.fromCharCode(a) + (b?String.fromCharCode(b):'') + (c?String.fromCharCode(c):'');
} while( i < string.length );
return result;
}
};
@Yaffle
Copy link

Yaffle commented Oct 13, 2011

@yahiko,
this algorithm returns wrong result on the following tests:

alert(Base64.encode(''));
alert(window.btoa(''));
alert(Base64.encode('a' + String.fromCharCode(0)));
alert(window.btoa('a' + String.fromCharCode(0)));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment