Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An example of base64 usage in cryptojs
var CryptoJS = require("crypto-js");//replace thie with script tag in browser env
//encrypt
var rawStr = "hello world!";
var wordArray = CryptoJS.enc.Utf8.parse(rawStr);
var base64 = CryptoJS.enc.Base64.stringify(wordArray);
console.log('encrypted:', base64);
//decrypt
var parsedWordArray = CryptoJS.enc.Base64.parse(base64);
var parsedStr = parsedWordArray.toString(CryptoJS.enc.Utf8);
console.log("parsed:",parsedStr);
@Lepozepo

This comment has been minimized.

Copy link

@Lepozepo Lepozepo commented Jun 1, 2015

Thanks for this gist ^_^

@kkworden

This comment has been minimized.

Copy link

@kkworden kkworden commented Jul 20, 2015

I hope you realized encoding something in Base64 isn't "encryption" lol

@gmale

This comment has been minimized.

Copy link

@gmale gmale commented Aug 26, 2015

Thanks for this. I used this example inside of Postman to help to parse JSON Web Tokens from one request and pass data through to another. Worked perfectly since CryptoJS is already included 👍

@embasa

This comment has been minimized.

Copy link

@embasa embasa commented Dec 10, 2015

Yea, It would be best if the words encrypt was changed to encode, and decrypt is changed to decode. It made me chuckle to read the word encrypt when encoding.

@albertosubero

This comment has been minimized.

Copy link

@albertosubero albertosubero commented Apr 21, 2016

Thanks for this 👍

@cbarrerah

This comment has been minimized.

Copy link

@cbarrerah cbarrerah commented Nov 17, 2016

Thanks, simple but usefull. ^.^

@umakannappan

This comment has been minimized.

Copy link

@umakannappan umakannappan commented Mar 2, 2017

Thanks a lot

@JohnMcDaniel

This comment has been minimized.

Copy link

@JohnMcDaniel JohnMcDaniel commented Jun 23, 2017

I also used this for a Postman test and it worked beautifully, thank you sir.

@zy723

This comment has been minimized.

Copy link

@zy723 zy723 commented Jun 27, 2017

Thanks

@Niefee

This comment has been minimized.

Copy link

@Niefee Niefee commented Nov 15, 2017

thinks ,i do not know why need CryptoJS.enc.Utf8.parse()?

@umeshramya

This comment has been minimized.

Copy link

@umeshramya umeshramya commented Jan 18, 2018

this does not convert JSON object

@MichelFloyd

This comment has been minimized.

Copy link

@MichelFloyd MichelFloyd commented Apr 3, 2018

Very helpful thank you!

@m47h4r

This comment has been minimized.

Copy link

@m47h4r m47h4r commented Jul 23, 2019

I hope you realized encoding something in Base64 isn't "encryption" lol

The title name should be changed to prevent confusion for those who are not really familiar.

@MichaelBurgess

This comment has been minimized.

Copy link

@MichaelBurgess MichaelBurgess commented Mar 1, 2020

this does not convert JSON object

If i've understood what you're saying, you just need to do this:

//encrypt
var myObj = { foo: "bar" };
var rawStr = JSON.stringify(myObj);
var wordArray = CryptoJS.enc.Utf8.parse(rawStr);
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.