1

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);

}
4

1 回答 1

3

swEncryptis a StreamWriter- 它的工作是将文本转换为二进制数据

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

msEncryptis a MemoryStream- 它的工作是将它提供的数据存储在内存中,以便您以后可以将其取出

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

于 2008-11-11T20:05:46.450 回答