---
title: "How do you encrypt/decrypt files using AES in C#?"  
description: "How do you encrypt/decrypt files using AES in C#?"  
author: "ICSM Computer"  
published: 2025-05-14  
updated: 2025-05-26  
canonical: https://www.mindstick.com/forum/161618/how-do-you-encrypt-decrypt-files-using-aes-in-c-sharp  
category: "c#"  
tags: ["c#", "file handling"]  
reading_time: 2 minutes  

---

# How do you encrypt/decrypt files using AES in C#?

How do you encrypt/decrypt [files](https://www.mindstick.com/articles/23302/the-importance-and-advantage-of-keeping-your-important-files-on-the-cloud) using [AES](https://answers.mindstick.com/qa/39353/which-state-government-has-launched-dastak-campaign-to-eradicate-deadly-acute-encephalitis-syndrome-aes-and-japanise-encephalitis-je-disease) in C#? with example

## Replies

### Reply by ICSM Computer

To **encrypt and decrypt files using AES** in C#, you can use the `Aes` class from the `System.Security.Cryptography` namespace. Below is a practical example demonstrating both encryption and decryption of files.

## 1. AES File Encryption in C#

### Encrypt a file

```cs
using System;
using System.IO;
using System.Security.Cryptography;

public class AesFileEncryption
{
    public static void EncryptFile(string inputFile, string outputFile, byte[] key, byte[] iv)
    {
        using FileStream inputStream = new FileStream(inputFile, FileMode.Open, FileAccess.Read);
        using FileStream outputStream = new FileStream(outputFile, FileMode.Create, FileAccess.Write);
        using Aes aes = Aes.Create();
        aes.Key = key;
        aes.IV = iv;

        using CryptoStream cryptoStream = new CryptoStream(outputStream, aes.CreateEncryptor(), CryptoStreamMode.Write);
        inputStream.CopyTo(cryptoStream);
    }
}
```

## 2. AES File Decryption in C#

### Decrypt a file

```cs
public class AesFileDecryption
{
    public static void DecryptFile(string inputFile, string outputFile, byte[] key, byte[] iv)
    {
        using FileStream inputStream = new FileStream(inputFile, FileMode.Open, FileAccess.Read);
        using FileStream outputStream = new FileStream(outputFile, FileMode.Create, FileAccess.Write);
        using Aes aes = Aes.Create();
        aes.Key = key;
        aes.IV = iv;

        using CryptoStream cryptoStream = new CryptoStream(inputStream, aes.CreateDecryptor(), CryptoStreamMode.Read);
        cryptoStream.CopyTo(outputStream);
    }
}
```

## 3. Generate AES Key and IV

```cs
using Aes aes = Aes.Create();
byte[] key = aes.Key;
byte[] iv = aes.IV;

// Save these securely; they are required for both encryption and decryption
```

> **Important**: Always securely store your AES `Key` and `IV`. Never hardcode them in production apps.

## Summary

| Task | Method |
| --- | --- |
| Encrypt | `AesFileEncryption.EncryptFile` |
| Decrypt | `AesFileDecryption.DecryptFile` |
| Key/IV Gen | `Aes.Create()` |


---

Original Source: https://www.mindstick.com/forum/161618/how-do-you-encrypt-decrypt-files-using-aes-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
