Decrypting the content of a json file

Hello,
I have made a connection with a RESTfull service.
It provide the response is a json file.
The content is encrypted and should be decrypted.
We also have a base64 SecureKey

It could look like (due to confidential data, i am not able to expose the real tag, and the content has been truncated).
{
“content”: “a5fdcf3b4d11d”,
“tag”: “c1b2f1169a71bc15bfb12a11bb3a1796,”,
“iv”: “d12b415cea13583f14a29141”,
“key_id”: “1”
}

The content is just Encrypted Plain text.
Tag and IV are both hexadecimal code.
And the securekey is base 64.

My question is how to decrypt this information within Studio 21.10.8 in windows compatibility mode.
I already tried the Uitpath.Cryptography.Activities.DecryptText activity.
This image shows a configuration interface for decrypting JSON content using the AES GCM algorithm with specified ciphertext and key variables. (Captioned by AI)

where we don’t need to provide the Tag and Iv, but i get the message:
Decrypt JSON content using AES GCM algorithm: A cryptographic operation has failed. Please make sure you use the same algorithm and key for both encryption and decryption operations.

I tried quite a lot, but i am not sure how to solve this issue.
I also tried to use some vb code, but for whatever reason this does not work properly.

The people that created the Service provides me with the below shown JS Node script.
When i fill in the data from the Json file between the “” or ‘’ i get a perfect result back.

So i hope that there is someone who can help me out with this issue.
Thanks in advance

const crypto = require(‘crypto’);

const encryptedText = “”;
const authTag = Buffer.from(‘’, ‘hex’);
const iv = Buffer.from(‘’, ‘hex’);
const key = Buffer.from(‘’, ‘base64’);

function decryptAES256GCM(encryptedText, key, iv, authTag) {
const decipher = crypto.createDecipheriv(‘aes-256-gcm’, key, iv);
decipher.setAuthTag(authTag);
let decrypted = decipher.update(encryptedText, ‘hex’, ‘utf8’);
decrypted += decipher.final(‘utf8’);
return decrypted;
}

// Example usage
try {
const decryptedText = decryptAES256GCM(encryptedText, key, iv, authTag);
console.log(‘Decrypted text:’, decryptedText);
} catch (err) {
console.error(‘Decryption failed:’, err.message);
}

@marco.vanzanten2

Please check this …looks like this is the issue.Please follow below format

Cheers

@Anil_G
I already saw this note, but that is related to the Windows Legacy compatibility mode.
The one i am using is in Windows compatibility mode.

So i am curious about what data i need to fill in into the variables of the activity.
The content from the JSON file is directly stored as a string into the strCipherText variable
And Base64 key it directly stored as a string into the strKey variable.

It might be that i need to convert the strKey, but i just don’t now which conversion type i need to use.

Besides that, it might be interesting to use an invoke code (vb.net or C#.net) activity, but than i am still questioning my self how to convert the different variables (when needed) to become arguments for the Invoke Code,
And the other question is, what should the code be.
An example that i found via ChatGPT related on the JSNode Script as show before could be (see below)

So if you have some suggestions, than please be my guest.

using System;
using System.Security.Cryptography;
using System.Text;

public class AESDecryption
{
public static string DecryptAES256GCM(byte encryptedText, byte key, byte iv, byte authTag)
{
string decryptedText = null;

    try
    {
        using (AesGcm aes = new AesGcm(key))
        {
            byte[] decryptedData = new byte[encryptedText.Length];
            aes.Decrypt(iv, encryptedText, authTag, decryptedData);
            decryptedText = Encoding.UTF8.GetString(decryptedData);
        }
    }
    catch (CryptographicException e)
    {
        Console.WriteLine($"Decryption failed: {e.Message}");
    }
    return decryptedText;
}

}

@marco.vanzanten2

Can you try to give all the values as mentioned in note itself…I beleive we need to pass all those values…and there is no other field…so trying it might bot be so bad

Cheers

Anil,
Luckily enough i found some C# code which is the solution for this issue.
Thanks a lot for your help.

The solution is:

byte content = Convert.FromHexString(in_strContent);
byte key = Convert.FromBase64String(in_strKey);
byte iv = Convert.FromHexString(in_strIv);
byte tag = Convert.FromHexString(in_strTag);

out_strDecryptedText = AESDecryption.DecryptAES256GCM(content, key, iv, tag);
System.Console.WriteLine($“Decrypted Text: {out_strDecryptedText}”);

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.