mert atayurt mert atayurt - 28 days ago 4
Node.js Question

Aes Encrypt and Decrypt null adds bytes

Hi there i'm using this encryption method to encrypt my json value in .net side

public static string Encrypt256(string text)
AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
aes.BlockSize = 128;
aes.KeySize = 256;
aes.IV = Encoding.UTF8.GetBytes(AesIV256);
aes.Key = Encoding.UTF8.GetBytes(AesKey256);
aes.Mode = CipherMode.CBC;

byte[] src = Encoding.Unicode.GetBytes(text);

using (ICryptoTransform encrypt = aes.CreateEncryptor())
byte[] dest = encrypt.TransformFinalBlock(src, 0, src.Length);
return Convert.ToBase64String(dest);

And im trying to decrypt it in Node Js side and
as you can see on the pic there are null bytes after every letter any idea why this is happening?


You are converting your text to be encrypted to Unicode which means UTF-16. In UTF-16 every character consists of two bytes. If the second byte is not used it is null as you have observed.

I assume you want UTF-8 encoding. Therefore replace the line

byte[] src = Encoding.Unicode.GetBytes(text);


byte[] src = Encoding.UTF8.GetBytes(text);