using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
var aes = new AesCryptoServiceProvider
{
Key = new byte[] {11, 22, 33, 44, 55, 66, 77, 88, 99, 100, 200, 123, 156, 34, 89, 93},
IV = new byte[] {34, 24, 32, 44, 55, 60, 13, 9, 22, 55, 77, 90, 23, 12, 13, 11}
};
var encryptor = aes.CreateEncryptor();
var decryptor = aes.CreateDecryptor();
//crypt
{
var text = Console.ReadLine();
var bytes = Encoding.UTF8.GetBytes(text);
// Тут вместо MemoryStream можно взять File.OpenRead, например
using var source = new MemoryStream(bytes);
using var destination = File.OpenWrite("file.txt.crypt");
Transform(source, destination, encryptor);
}
Console.WriteLine("---- See result in file.txt.crypt ----");
Console.WriteLine("<Press ENTER to continue>");
Console.ReadLine();
//decrypt
{
using var source = File.OpenRead("file.txt.crypt");
// тут, вместо stdout можно взять File.OpenWrite
using var destination = Console.OpenStandardOutput();
Transform(source, destination, decryptor);
}
static void Transform(Stream source, Stream destination, ICryptoTransform encryptor)
{
using var cryptoStream = new CryptoStream(destination, encryptor, CryptoStreamMode.Write);
source.CopyTo(cryptoStream);
}