2

I am confused about this topic when it comes to AES encryption.

The MSDN page shows quite a lot of code used to encrypt/decrypt and uses multiple streams:

// Create the streams used for encryption.
                using (MemoryStream msEncrypt = new MemoryStream())
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                        {
                            //Write all data to the stream.
                            swEncrypt.Write(plainText);
                        }
                        encrypted = msEncrypt.ToArray();
                    }
                }
            }

While the video on encryption from MS shows all of this done in a single line:

byte[] plainText = cryptTransform.TransofmrFinalBlock(cipherText, 0, cipherText.Length);

Why would I use the former over the latter? Is there something the first one does that the second on can't?

dksue
  • 61
  • 3
  • 1
    Does this answer your question? [Why use CryptoStream instead of TransformBlock?](https://stackoverflow.com/questions/51873329/why-use-cryptostream-instead-of-transformblock) – Topaco Jun 09 '20 at 10:19
  • @user9014097 Sort of, though that answer is not very clear. – Suncat2000 Jul 06 '21 at 17:12

0 Answers0