문제

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