Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

220
Views
Why AEC encryption in .NET yields different result than JavaScript?

I'm going crazy about this one. Spend whole day and still can't understand what is going on. I'm using AES256CBC encryption both in .Net and JavaScript. For some reason I got different results, despite that I'm using same key an iv. My codes are:

JavaScript:

function convertStringToArrayBuffer(str) {
        var length = str.length;
        var bytes = new Uint8Array(length);
        for(var i = 0; i < length; i++) {
            bytes[i] = str.charCodeAt(i);
        }
        return bytes;
    }

    var keyB64 ="sy/d1Ddy/9K3p8x6pWMq2P8Qw2ftUjkkrAA7xFC7aK8=";
    var viB64 = "t8eI2F+QmlUBWZJVIlTX6Q==";

    var dataToEnc = "Test123!"
    let dataInBytes = convertStringToArrayBuffer(dataToEnc);

    let key = window.atob(keyB64);
    let iv = window.atob(viB64);

    console.log(key);
    console.log(iv);
    window.crypto.subtle.importKey("raw", convertStringToArrayBuffer(key).buffer, {name: "AES-CBC", length: 256}, false, ["encrypt"]).then(function(key){
        console.log(key);
        window.crypto.subtle.encrypt({name: "AES-CBC", iv: convertStringToArrayBuffer(iv).buffer}, key, dataInBytes.buffer).then(function(encrypted){
            console.log(encrypted);
        });
    });

This one produces

enter image description here

.Net:

public static void Test()
        {
            var dataToEnc = "Test123!";
            var keyB64 = "sy/d1Ddy/9K3p8x6pWMq2P8Qw2ftUjkkrAA7xFC7aK8=";
            var viB64 = "t8eI2F+QmlUBWZJVIlTX6Q==";
            var key = Convert.FromBase64String(keyB64);
            var iv = Convert.FromBase64String(viB64);
            var data = Encoding.UTF8.GetBytes(dataToEnc);

            byte[] encrypted = null;
            using (Aes aesAlg = Aes.Create())
            {
                aesAlg.Key = key;
                aesAlg.IV = iv;

                ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
                using (MemoryStream msEncrypt = new MemoryStream())
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                        {
                            swEncrypt.Write(data);
                        }
                        encrypted = msEncrypt.ToArray();
                    }
                }

            }

        }

This one produces

enter image description here

I belive it is something trivial, yet I can't find this. I appreciate any hint here.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If anyone else is having this issue @Topaco's comment is right way to go, I'm pasting it below

The bug is in the C# code. You have to use swEncrypt.Write(dataToEnc) instead of swEncrypt.Write(data). The overload you are currently using implicitly executes data.ToString()

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!