这是我的一段代码,它应该加密文件并将密码写入新文件。代码类似于 Microsoft MSDN 网页上的示例。
byte[] inputBuffer = new byte[InputFileStream.Length];
byte[] outputBuffer = new byte[InputFileStream.Length];
InputFileStream.Read(inputBuffer, 0, (int)InputFileStream.Length);
ICryptoTransform encryptor = AesInstance.CreateEncryptor(AesInstance.Key, AesInstance.IV);
using (MemoryStream memoryStream = new MemoryStream())
{
using (CryptoStream stream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter encrypted = new StreamWriter(stream))
{
encrypted.Write(inputBuffer);
}
outputBuffer = memoryStream.ToArray();
OutputFileStream.Write(outputBuffer, 0, (int)outputBuffer.Length);
}
}
OutputFileStream.Close();
InputFileStream.Close();`
问题是输入缓冲区从文件中正确读取字节,但输出缓冲区似乎只有 16 个字节的数据。结果是输出文件只有 16 个字节,即使输入文件很大。错误在哪里?各种模式,ECB,CBC等都存在问题。