What does Streams do when implementing AES in .NET?

The Rijndael encryption algorithm is implemented in .NET using three streams in the following example: Rinjdael .

Can someone explain to me what these threads are doing? How / Why are they used?

// Declare the streams used
// to encrypt to an in memory
// array of bytes.
MemoryStream msEncrypt = null;
CryptoStream csEncrypt = null;
StreamWriter swEncrypt = null;

// Declare the RijndaelManaged object
// used to encrypt the data.
RijndaelManaged aesAlg = null;

try
{
    // Create a RijndaelManaged object
    // with the specified key and IV.
    aesAlg = new RijndaelManaged();
    aesAlg.Key = Key;
    aesAlg.IV = IV;


    // Create a encryptor to perform the stream transform.
    ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

    // Create the streams used for encryption.
    msEncrypt = new MemoryStream();
    csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
    swEncrypt = new StreamWriter(csEncrypt);

    //Write all data to the stream.
    swEncrypt.Write(plainText);

}

      

+1


source to share


1 answer


swEncrypt

is StreamWriter

- its task is to convert text to binary data

csEncrypt

is CryptoStream

- its job is to convert binary data to encrypted binary data



msEncrypt

- it's MemoryStream

- its job is to store data, data in memory, so you can get it later

When you put them all together, you basically end up with something where you can write plain text at one end and receive encrypted binary data (temporarily storing it in memory) at the other end.

+3


source







All Articles