Forum Discussion

Roshantha's avatar
Roshantha
New Contributor
2 years ago
Solved

Encrypt/Decrypt Base64 string in TestComplete

Hi,

 

I'm in need of decrypting a Base64 string in a Testcomplete script. The value is coming from a datafile. Is it possible to use a javascript method like atob() or btoa(), within testcomplete for this purpose? Or is there any other option?

 

Thanks,

~Roshan

  • Hi rraghvani, thanks for the suggestion. It seemed working. However, I wanted to convert using 'FromBase64String', which gave me the byte array of the converted string. And had troubles converting the byte array to the string. At the and created following function to invoke a PowerShell command. That did the trick for me. 

     

    function b64ToString (str){

    var ps = dotNET.System_Management_Automation.PowerShell.Create();

    ps.AddScript( "[Text.Encoding]::Utf8.GetString([Convert]::FromBase64String('"+str+"')) | Out-String");

    var results = ps.Invoke();

    for (i = 0; i < results.Count; i++)
    {
    convertedString = results.Item(i).ImmediateBaseObject;

    }

    return convertedString;

    }

     

2 Replies

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    If you have dotNet extension installed, you could try something like

     

    var encodedText = dotNET.System.Convert.ToBase64String("Hello World"); 
    • Roshantha's avatar
      Roshantha
      New Contributor

      Hi rraghvani, thanks for the suggestion. It seemed working. However, I wanted to convert using 'FromBase64String', which gave me the byte array of the converted string. And had troubles converting the byte array to the string. At the and created following function to invoke a PowerShell command. That did the trick for me. 

       

      function b64ToString (str){

      var ps = dotNET.System_Management_Automation.PowerShell.Create();

      ps.AddScript( "[Text.Encoding]::Utf8.GetString([Convert]::FromBase64String('"+str+"')) | Out-String");

      var results = ps.Invoke();

      for (i = 0; i < results.Count; i++)
      {
      convertedString = results.Item(i).ImmediateBaseObject;

      }

      return convertedString;

      }