Skip to content

Instantly share code, notes, and snippets.

@joecliff
Last active March 11, 2024 08:00
Show Gist options
  • Star 87 You must be signed in to star a gist
  • Fork 21 You must be signed in to fork a gist
  • Save joecliff/10948117 to your computer and use it in GitHub Desktop.
Save joecliff/10948117 to your computer and use it in GitHub Desktop.
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);
@umeshramya
Copy link

this does not convert JSON object

@MichelFloyd
Copy link

Very helpful thank you!

@MichaelBurgess
Copy link

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);
...

@D0LLYNH0
Copy link

D0LLYNH0 commented Jan 2, 2022

Version with only one line each:

var CryptoJS = require("crypto-js"); //replace thie with script tag in browser env

//
var rawStr = "hello world!";

//encrypt
var base64 = CryptoJS.enc.Utf8.parse(rawStr).toString(CryptoJS.enc.Base64);

//decrypt
var parsedStr = CryptoJS.enc.Base64.parse(base64).toString(CryptoJS.enc.Utf8);

@YevheniiPokhvalii
Copy link

YevheniiPokhvalii commented Jan 9, 2022

Someone may find it useful.
I noticed that Kubernetes encodes ssh private and public keys in its secrets (may SSL keys too) with base64 adding the trailing line at the end of a key.
For example, CryptoJS works the way similar to

$ echo -n 'Hello World' | base64
$ SGVsbG8gV29ybGQ=

-n means that it does not add a trailing line. While keys in Kubernetes are encoded using a trailing line at the end (you can see a different result)

$ echo 'Hello World' | base64
$ SGVsbG8gV29ybGQK

So to make sure the keys are encoded the way it is meant by Kubernetes I tried the solution in Postman with a new line character - (plainText + '\n'):

var plainText = pm.environment.get('github_ssh_private_key');
var encoded = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(plainText + '\n'));
pm.environment.set("github_ssh_private_key_base64", encoded);

@cirsolar
Copy link

awesooooom

@Rockydaniel
Copy link

this saves my day a lot, thanks!

@stevenyeo99
Copy link

stevenyeo99 commented Feb 16, 2022

Hallo can someone explain me the flow also how to decrypt back, if that was encrypted this way?

i am confused currently as still new to cryptography

// Code Section
{

function encrypt(data) {

const val = cryptoJS.enc.Utf8.parse(JSON.stringify(data));

const encrypted = cryptoJS.AES.encrypt(val, key, { iv: IV }).toString();

let b64 = cryptoJS.enc.Base64.parse(encrypted).toString(cryptoJS.enc.Hex);

return b64;

}
}

@n1ghtmare
Copy link

This gist saved me! Thank you so much!

@zhangxin-lab
Copy link

thanks for this

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