Question

I'm trying to encrypt a buffer with RijndaelManaged class without any success. It always return byte[0]. Here's the code:

    public byte[] Encrypt(byte[] data, byte[] key)
    {
        using (var ms = new MemoryStream())
        {
            using (var aes = RijndaelManaged.Create())
            {
                aes.Key = _checksumProvider.CalculateChecksum(key);
                aes.IV = _checksumProvider.CalculateChecksum(key);
                var stream = new CryptoStream(ms, aes.CreateEncryptor(aes.Key, aes.IV), CryptoStreamMode.Write);
                stream.Write(data, 0, data.Length);
                return ms.ToArray();
            }
        }
    }

Key and IV are correctly assigned. Any idea what's wrong with the code? Thanks.

Was it helpful?

Solution

You need to call stream.FlushFinalBlock().

This will perform any final steps in the encryption, and flush the CryptoStream's internal buffer into the underlying memory stream.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top