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.

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top