Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

225
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda