我的要求如下。步骤 1)我想通过以 10-10 MB 的块加密来在本地文件系统中写入 101 MB 的数据。
第 2 步)解密时,我想从文件中获取 10-10 MB 的数据并想解密并希望将 10-10 MB 解密后的数据传递给其他函数(注意:我的最后一个块大小为 1 MB,因为文件大小为101 MB)。
所以
1)当我尝试仅解密 10 MB 时,它会给出错误“填充无效且无法删除”
2)现在,在解密时,如果我将 paddingMode 设置为 None,那么当最后一个块出现时,即 1 MB,它会给出错误“要解密的数据长度无效”。高达 100 MB 的东西正在运行
注意:1) 最多 100 MB 的东西按预期工作。如果我给最后一个块大小 10 MB 而不是 1 MB 那么它的工作但最后我的目标文件大小将是 110 MB 而不是 101 MB。2)我在加密时没有给出填充模式,而是为了避免填充无效错误,而在解密时我没有给出它。临时我已经评论了该代码。
3)我使用相同的密钥和IV进行加密和解密
加密代码:
private void WriteStreamInChunks(Stream fsstream, string filePath)
{
int chunkSize = 10 * 1024 * 1024;
byte[] buffer = new byte[chunkSize];
using (var rijndaelManaged = new RijndaelManaged())
{
var encryptor = rijndaelManaged.CreateEncryptor(EncryptionKey, EncryptionIV);
using (var fileStream = File.Create(filePath))
{
using (var cryptoStream = new CryptoStream(fileStream, encryptor, CryptoStreamMode.Write))
{
int bytesRead;
while ((bytesRead = fsstream.Read(buffer, 0, chunkSize)) > 0)
{
cryptoStream.Write(buffer, 0, bytesRead);
}
if (!cryptoStream.HasFlushedFinalBlock)
cryptoStream.FlushFinalBlock();
}
}
}
}
仅解密块并返回 byte[] 代码 - 错误
如果我将填充模式设置为零然后数据长度无效异常,则“填充无效”。
public byte[] GetDecryptedFileContent(string filePath, long chunkSizeInBytes, long seekValue, long fileSize, string encryptionIV)
{
var sourceFile = new FileInfo(filePath);
var buffer = new byte[chunkSizeInBytes];
byte[] encryptionKey = File.ReadAllBytes(Utils.GetSymmetricAlgoEncryptionKey());
using (var fileStream = File.Open(sourceFile.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
fileStream.Seek(seekValue, SeekOrigin.Begin);
using (var binaryReader = new BinaryReader(fileStream))
{
binaryReader.Read(buffer, 0, buffer.Length);
}
}
using (MemoryStream dataOut = new MemoryStream())
{
using (RijndaelManaged rijndaelManaged = new RijndaelManaged())
{
//rijndaelManaged.Padding = PaddingMode.None;
ICryptoTransform decryptor = rijndaelManaged.CreateDecryptor(encryptionKey, Convert.FromBase64String(encryptionIV));
using (CryptoStream cryptoStream = new CryptoStream(dataOut, decryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(buffer, 0, buffer.Length);
return dataOut.ToArray();
}
}
}
return null;
}
根据我的要求,我必须从这个 GetDecryptedFileContent 函数返回 byte[] 。我多次调用此函数,直到文件大小变为 0。即对于每个块。enter code here
有人可以帮帮我吗?
我只想返回 10-10 MB。