function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ 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,
P
Best Answer chosen by Phuc 2
Alain CabonAlain Cabon

https://cryptojs.gitbook.io/docs/
 
Static Resource Detail
Name	cryptojs
Namespace Prefix	
Description	
MIME Type	text/javascript
Cache Control	Private
Size	48 316 bytes
 
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome" access="global" >
	<ltng:require 
    scripts="{!$Resource.cryptojs}"
    afterScriptsLoaded="{!c.scriptsLoaded}" />
    <h1>Crypto Lex</h1>
</aura:component>
 
({
    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)

 

All Answers

Alain CabonAlain Cabon

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

https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js

https://salesforce.stackexchange.com/questions/368078/encrypt-file-using-crypto-js-in-aura-component

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.

https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/security_locker_console_evaluate.htm

https://developer.salesforce.com/docs/component-library/tools/locker-service-console

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.
P
Alain CabonAlain Cabon

https://cryptojs.gitbook.io/docs/
 
Static Resource Detail
Name	cryptojs
Namespace Prefix	
Description	
MIME Type	text/javascript
Cache Control	Private
Size	48 316 bytes
 
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome" access="global" >
	<ltng:require 
    scripts="{!$Resource.cryptojs}"
    afterScriptsLoaded="{!c.scriptsLoaded}" />
    <h1>Crypto Lex</h1>
</aura:component>
 
({
    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)

 
This was selected as the best answer