【发布时间】:2011-11-24 10:01:49
【问题描述】:
我有一个使用对称加密加密和解密数据的算法。无论如何,当我要解密时,我有:
CryptoStream cs = new CryptoStream(ms, cryptoTransform, CryptoStreamMode.Read);
我必须从 cs CryptoStream 读取数据并将该数据放入字节数组中。所以一种方法可能是:
System.Collections.Generic.List<byte> myListOfBytes = new System.Collections.Generic.List<byte>();
while (true)
{
int nextByte = cs.ReadByte();
if (nextByte == -1) break;
myListOfBytes.Add((Byte)nextByte);
}
return myListOfBytes.ToArray();
另一种技术可能是:
ArrayList chuncks = new ArrayList();
byte[] tempContainer = new byte[1048576];
int tempBytes = 0;
while (tempBytes < 1048576)
{
tempBytes = cs.Read(tempContainer, 0, tempContainer.Length);
//tempBytes is the number of bytes read from cs stream. those bytes are placed
// on the tempContainer array
chuncks.Add(tempContainer);
}
// later do a for each loop on chunks and add those bytes
我无法提前知道流 cs 的长度:
或者我应该实现我的堆栈类。我将加密大量信息,因此使这段代码高效将节省大量时间
【问题讨论】:
标签: c# performance stream