Solved

Javascript Encryption Function not running on K2

  • 2 August 2022
  • 5 replies
  • 10 views

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

 

icon

Best answer by ernie_hayter 3 August 2022, 11:53

View original

5 replies

Userlevel 2
Badge +6

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

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>

 

Userlevel 2
Badge +6

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







@tobyCodes

Thanks Ernie, really appreciate your help. This works perfectly fine!
Cheers..
Userlevel 2
Badge +6

Happy it worked!

Reply