tobyCodes
Rookie

Javascript Encryption Function not running on K2

Jump to solution

Hello everyone, I'm trying to run a Javascript function that encrypts the values from a textbox and display the output on another input / data label for starters, I've attempted several methods and previous solutions from past posts but none works as the function just doesn't run on k2.

 

I cannot figure out how to make the function run and display the result. I feel like I'm missing something somewhere.

Any help or pointers would be very much appreciated. 

Thanks

 

Labels: (2)
0 Kudos
Reply
5 Replies
ernie_hayter
Apprentice

Re: Javascript Encryption Function not running on K2

Jump to solution

@tobyCodes if you share the JS you are trying to execute, maybe I can assist?

0 Kudos
Reply
tobyCodes
Rookie

Re: Javascript Encryption Function not running on K2

Jump to solution

Hi @ernie_hayter Thanks for reaching out. Tried several methods and also used cryptoJs library, here's the cryptoJs code:

<script> CryptoJS.AES.encrypt("data", "passphrase").toString();</script>

and cdn link: 

<script type="text/javascript">var s = document.createElement("script"); s.type = "text/javascript"; s.src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/aes.js";$("head").append(s);</script>

 below is also a function that does simple js encryption:

<script text="text/javascript">$(function encryptValue() { var data = TextBox; var encrypted = (data) => { const salt = "secret"; const textToChars = (data) => data.split("").map((c) => c.charCodeAt( 0)); const byteHex = (n) => ("0" + Number(n).toString(16)).substr(- 2); const applySaltToChar = (code) => textToChars(salt).reduce((a, b) => a ^ b, code); 
return data .split("") .map(textToChars) .map(applySaltToChar) .map(byteHex) .join(""); } return encrypted(data); } 
encryptValue());</script>

 

0 Kudos
Reply
ernie_hayter
Apprentice

Re: Javascript Encryption Function not running on K2

Jump to solution

Hi   

I was able to get this working just fine. Here's a breakdown of what I did:

 

  1. Add a data label to the canvas with Literal = true and Prevent XSS = false
  2. Add <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/aes.js" ></script> as the text of the data label (no need to add it to the head of the page, as it will not be evaluated there post load)
  3. Add 2 text boxes, a button and another data label (Literal = true, Prevent XSS = false)
  4. On button click transfer this to the second data label: <script>var encryptedValue = CryptoJS.AES.encrypt("data", "<<Map your Text Box>>").toString(); $("[name='<<Name of Result Text Box>>']").SFCTextBox("option", "text", encryptedValue)</script>
  5. If you are planning on running this same code multiple times, add an OnChange rule to the second data label that will just empty its own value, that way the code will evaluate multiple times

     

    Screenshots for reference.

     

    Let me know if you have any questions.

     

    Cheers,

    Ernie

    ernie_hayter_2-1659520285163.png

    ernie_hayter_1-1659520249649.png

    ernie_hayter_0-1659520186565.png

@tobyCodes

Reply
tobyCodes
Rookie

Re: Javascript Encryption Function not running on K2

Jump to solution
Thanks Ernie, really appreciate your help. This works perfectly fine!
Cheers..
0 Kudos
Reply
ernie_hayter
Apprentice

Re: Javascript Encryption Function not running on K2

Jump to solution

Happy it worked!

0 Kudos
Reply