RJ TextEd is a full featured text and source editor with Unicode support. It is also a very powerful web (PHP, ASP, JavaScript, HTML and CSS) development editor. The functionality extends beyond text files and includes support for CSS/HTML editing with integrated CSS/HTML preview, spell checking, auto completion, HTML validation, templates and more.
Are there any methods in JavaScript that could be used to encode and decode a string using base64 encoding?
11 Answers
Some browsers such as Firefox, Chrome, Safari, Opera and IE10+ can handle Base64 natively. Take a look at this Stackoverflow question. It's using btoa()
and atob()
functions.
For server-side JavaScript, there's a btoa package for Node.JS.
If you are going for a cross-browser solution, there are existing libraries like CryptoJS or code like:
With the latter, you need to thoroughly test the function for cross browser compatibility. And error has already been reported.
In Gecko/WebKit-based browsers (Firefox, Chrome and Safari) and Opera, you can use btoa() and atob().
Original answer: How can you encode a string to Base64 in JavaScript?
Internet Explorer 10+
Cross-Browser
with Node.js
Here is how you encode normal text to base64 in Node.js:
And here is how you decode base64 encoded strings:
with Dojo.js
To encode an array of bytes using dojox.encoding.base64:
To decode a base64-encoded string:
bower install angular-base64
But How?
If you would like to learn more about how base64 is encoded in general, and in JavaScript in-particular, I would recommend this article: Computer science in JavaScript: Base64 encoding
davidcondreydavidcondreyHere is a tightened up version of Sniper's post. It presumes well formed base64 string with no carriage returns. This version eliminates a couple of loops, adds the &0xff
fix from Yaroslav, eliminates trailing nulls, plus a bit of code golf.
Short and fast Base64 JavaScript Decode Function without Failsafe:
msrd0The php.js project has JavaScript implementations of many of PHP's functions. base64_encode
and base64_decode
are included.
Did someone say code golf? =)
The following is my attempt at improving my handicap while catching up with the times. Supplied for your convenience.
What I was actually after was an asynchronous implementation and to my surprise it turns out forEach
as opposed to JQuery's $([]).each
method implementation is very much synchronous.
If you also had such crazy notions in mind a 0 delay window.setTimeout
will run the base64 decode asynchronously and execute the callback function with the result when done.
@Toothbrush suggested 'index a string like an array', and get rid of the split
. This routine seems really odd and not sure how compatible it will be, but it does hit another birdie so lets have it.
While trying to find more information on JavaScript string as array I stumbled on this pro tip using a /./g
regex to step through a string. This reduces the code size even more by replacing the string in place and eliminating the need of keeping a return variable.
If however you were looking for something a little more traditional perhaps the following is more to your taste.
I didn't have the trailing null issue so this was removed to remain under par but it should easily be resolved with a trim()
or a trimRight()
if you'd prefer, should this pose a problem for you.
ie.
Note:
The result is an ascii byte string, if you need unicode the easiest is to escape
the byte string which can then be decoded with decodeURIComponent
to produce the unicode string.
Since escape
is being deprecated we could change our function to support unicode directly without the need for escape
or String.fromCharCode
we can produce a %
escaped string ready for URI decoding.
nJoy!
nickl-nickl-I have tried the Javascript routines at phpjs.org and they have worked well.
I first tried the routines suggested in the chosen answer by Ranhiru Cooray - http://ntt.cc/2008/01/19/base64-encoder-decoder-with-javascript.html
I found that they did not work in all circumstances. I wrote up a test case where these routines fail and posted them to GitHub at:
I also posted a comment to the blog post at ntt.cc to alert the author (awaiting moderation - the article is old so not sure if comment will get posted).
I'd rather use the bas64 encode/decode methods from CryptoJS, the most popular library for standard and secure cryptographic algorithms implemented in JavaScript using best practices and patterns.
Dan DascalescuDan Dascalescu