Streams在.NET中实现AES加密时会做什么?

时间:2008-11-11 19:53:52

标签: .net encryption stream aes rijndaelmanaged

Rijndael加密算法在.NET中使用以下示例中的3个流实现:Rinjdael

有人可以向我解释这些流正在做什么吗?他们是如何/为何使用?

// 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 个答案:

答案 0 :(得分:3)

swEncryptStreamWriter - 其工作是将文本转换为二进制数据

csEncryptCryptoStream - 它的工作是将二进制数据转换为加密的二进制数据

msEncrypt是一个MemoryStream - 它的工作是将它在内存中提供的数据存储起来,以便稍后将其删除

当你把它们放在一起时,你基本上得到的东西是你可以在一端写明文,并获得另一端的加密二进制数据(暂时存储在内存中)。