Pre-response Script - decode response before displaying it in Body window

I’m trying to move my entire workflow into Postman.
Endpoint I’m trying to use is returning encrypted responses for anything that is returned with status code 200.
I have a C# app that I use to test this endpoint (to see decoded response) but I want to use Postman.

Currently, there is a pre-request script, but is there a pre-response script? I would like to check response status code and if it is OK (200) I would like to decode response.

Inside C# I use this code to encrypt/decrypt response in my test app:

public static string EncryptRijndael(string input, string key)
{
    var aes = new RijndaelManaged
    {
        KeySize = 256,
        BlockSize = 256,
        Padding = PaddingMode.PKCS7,
        Mode = CipherMode.CBC,
        Key = Encoding.UTF8.GetBytes(key)
    };

    aes.GenerateIV();

    var encryptor = aes.CreateEncryptor(aes.Key, aes.IV);

    byte[] buffer;
    using (var ms = new MemoryStream())
    {
        using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
        {
            byte[] bytes = Encoding.UTF8.GetBytes(input);
            cs.Write(bytes, 0, bytes.Length);
        }
        buffer = ms.ToArray();
    }

    buffer = buffer.Concat(aes.IV).ToArray();

    aes.Dispose();
    return Convert.ToBase64String(buffer);
}

public static string DecryptRijndael(string input, string key)
{
    var aes = new RijndaelManaged
    {
        KeySize = 256,
        BlockSize = 256,
        Mode = CipherMode.CBC,
        Padding = PaddingMode.PKCS7,
        Key = Encoding.UTF8.GetBytes(key)
    };

    byte[] xXml = Convert.FromBase64String(input);
    var buffer = xXml.Take(xXml.Length - aes.IV.Length).ToArray();
    var iv = xXml.Skip(xXml.Length - aes.IV.Length).ToArray();

    aes.IV = iv;
    var decrypt = aes.CreateDecryptor();
    byte[] xBuff;
    using (var ms = new MemoryStream())
    {
        using (var cs = new CryptoStream(ms, decrypt, CryptoStreamMode.Write))
        {
            cs.Write(buffer, 0, buffer.Length);
        }
        xBuff = ms.ToArray();
    }
    aes.Dispose();
    String output = Encoding.UTF8.GetString(xBuff);
    return output;
}

Hello
did you achieve your goal?
if is YES please share =)

You can’t change the response body, but if you wanna avoid console.log’ing from a test you can simply use pm.visualizer.set(…) to write something into the visualize tab.
There you can write html code which gets rendered, so as long your response is formatted to html you can print it in a formatted form similiar to the raw tab.
Assuming your decrypted response body, which is in json, is inside the decData variable your code could be as follow:

const decData = '{"a": "foo", "b": [1,2,3,"bar"]}';
const dataParsed = JSON.parse(decData);
console.log(dataParsed);
const formattedDataHtmlFormat = JSON.stringify(dataParsed, null, 2).replace(/ /g, "&nbsp;").replace(/\n/g, "<br />");
pm.visualizer.set(formattedDataHtmlFormat);

Going from here you could also do some formatting to get some colored html output for code highlighting, but for being able to see it in a formatted structure was good enough.