У меня есть файлы, хранящиеся локально и зашифрованные с помощью aes.
Мне нужен один из документы в коде в расшифрованном виде в виде потока.
Я пробую туда-сюда и не могу заставить его работать.
Как должен выглядеть метод, чтобы что я получу поток обратно с расшифрованным контентом.
Код: Выделить всё
public Stream DecryptFile(string filePath){..??..}
Код: Выделить всё
public async Task EncryptAndSaveFileAsync(Stream stream, string filePath)
{
await using var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write);
var tempFilePath = Path.GetTempFileName();
try
{
if (!stream.CanRead)
{
stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
}
await using (var tempFileStream = new FileStream(tempFilePath, FileMode.Create, FileAccess.Write))
{
await stream.CopyToAsync(tempFileStream);
}
fileStream.Close();
try
{
await using var inputFileStream = new FileStream(tempFilePath, FileMode.Open, FileAccess.Read);
await using (var outputFileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
using var aesAlg = Aes.Create();
aesAlg.Key = _key;
aesAlg.IV = new byte[16];
await using var cryptoStream = new CryptoStream(outputFileStream, aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV), CryptoStreamMode.Write);
await inputFileStream.CopyToAsync(cryptoStream);
}
inputFileStream.Close();
}
catch (Exception ex)
{
Console.WriteLine("Fehler beim Verschlüsseln der Datei: " + ex.Message);
}
}
catch (Exception ex)
{
// TODO FM Logging
}
finally
{
File.Delete(tempFilePath);
}
}
Источник: https://stackoverflow.com/questions/781 ... filestream