Last active
October 10, 2024 19:44
-
-
Save joecliff/10948117 to your computer and use it in GitHub Desktop.
An example of base64 usage in cryptojs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
this saves my day a lot, thanks!
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;
}
}
This gist saved me! Thank you so much!
thanks for this
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
awesooooom