public static void Main()
{
string path = "ali.txt";
byte[] key = { 11, 22, 33, 44, 55, 66, 77, 88, 99, 100, 200, 123, 156, 34, 89, 93 };
byte[] iv = { 34, 24, 32, 44, 55, 60, 13, 9, 22, 55, 77, 90, 23, 12, 13, 11 };
byte[] gang;
// ШИФРАТОР
using (SymmetricAlgorithm aesAlg = Aes.Create())
{
using (ICryptoTransform encryptor = aesAlg.CreateEncryptor(key, iv))
{
gang = getArray(path);
using (FileStream fsCrypt = new FileStream(path, FileMode.Create))
{
using (CryptoStream cs = new CryptoStream(fsCrypt, encryptor, CryptoStreamMode.Write))
{
cs.Write(gang, 0, gang.Length);
}
}
}
}
// ДЕШИФРАТОР
StreamReader sr = null;
using (SymmetricAlgorithm alghoritm = Aes.Create())
{
using (ICryptoTransform decryptor = alghoritm.CreateDecryptor(key, iv))
{
using (FileStream stream = new FileStream(path, FileMode.Open))
{
using (CryptoStream crypto = new CryptoStream (stream, decryptor, CryptoStreamMode.Read))
{
sr = new StreamReader(crypto);
Console.WriteLine(sr.ReadToEnd());
}
}
}
}
}
static byte[] getArray (string path)
{
byte[] gang
UnicodeEncoding UE = new UnicodeEncoding();
using (FileStream fs = new FileStream(path, FileMode.Open))
{
using (StreamReader sra = new StreamReader(fs))
{
gang= UE.GetBytes(sra.ReadToEnd());
return gang;
}
}
}
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);
}