Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

221
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda