As said in the title:
using System.IO;
using System.Security.Cryptography;
class Program
{
static void Main()
{
string inputFile = "your_file.exe"; // replace with your file name/path
string outputFile = "encrypted_file.exe"; // set the encrypted file name
// create random AES key (for symmetric encryption)
byte[] key = new byte[32];
using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
{
rng.GetBytes(key);
}
// read in the input file
byte[] bytesToEncrypt = File.ReadAllBytes(inputFile);
// encrypt the input file bytes with AES
byte[] encryptedBytes = null;
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = key;
aesAlg.GenerateIV();
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
csEncrypt.Write(bytesToEncrypt, 0, bytesToEncrypt.Length);
csEncrypt.FlushFinalBlock();
encryptedBytes = msEncrypt.ToArray();
}
}
}
// write out the encrypted bytes to a file
File.WriteAllBytes(outputFile, encryptedBytes);
}
}
using System.IO;
using System.Security.Cryptography;
class Program
{
static void Main()
{
string inputFile = "your_file.exe"; // replace with your file name/path
string outputFile = "encrypted_file.exe"; // set the encrypted file name
// create random AES key (for symmetric encryption)
byte[] key = new byte[32];
using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
{
rng.GetBytes(key);
}
// read in the input file
byte[] bytesToEncrypt = File.ReadAllBytes(inputFile);
// encrypt the input file bytes with AES
byte[] encryptedBytes = null;
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = key;
aesAlg.GenerateIV();
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
csEncrypt.Write(bytesToEncrypt, 0, bytesToEncrypt.Length);
csEncrypt.FlushFinalBlock();
encryptedBytes = msEncrypt.ToArray();
}
}
}
// write out the encrypted bytes to a file
File.WriteAllBytes(outputFile, encryptedBytes);
}
}
Последнее редактирование: