+ Start a Discussion
Phuc 2Phuc 2 

Create encrypt string in Aura component

Hello all,
I have an aura component where I need to encrpty a string that contains vlaues from the component.  Can I do this within the component JS?  Or does the data need to be sent to a controller and then pulled back again?  I am encrypting  a string and sending it to an external site when a user saves their record.
Thank you,
Alain CabonAlain Cabon

1) Import crypto-js.min.js as a static resource



2) No problem detected from the locker console

Ensure that your code is compatible with Lightning Locker by running the code with Locker enabled. Run the code a second time with Locker disabled to see if any errors are due to Lightning Locker restrictions.



User-added image

Phuc 2Phuc 2
Thank you for teh reply Alain.
So after add the crypto-js.min.js file as a static resource.
How do I pass in the values to encrypt?  CryptoJS.AES.encrypt('Name', 'address');
This can be done in the helper JS correct?
Thanks for your help.
Alain CabonAlain Cabon

Static Resource Detail
Name	cryptojs
Namespace Prefix	
MIME Type	text/javascript
Cache Control	Private
Size	48 316 bytes
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome" access="global" >
    afterScriptsLoaded="{!c.scriptsLoaded}" />
    <h1>Crypto Lex</h1>
    scriptsLoaded : function(component, event, helper) {
        var encrypted = CryptoJS.AES.encrypt("Message", "Secret Passphrase");
        console.log('> encrypted:' + encrypted);
        var decrypted = CryptoJS.AES.decrypt(encrypted, "Secret Passphrase").toString(CryptoJS.enc.Utf8);
        console.log('> decrypted:' + decrypted);

> encrypted:U2FsdGVkX1/JqGDagBEnmRXSuDmYb5MNL7Kuo4xT+gk=  ( "Message" encoded )
> decrypted:Message  (ok)