Introduction
The Advanced Encryption Standard (AES), also known as Rijndael Encryption, was developed by two Belgian cryptographers, Vincent Rijmen and Joan Daemen. AES gives a feature to share or store your files in a highly secure manner. In this blog we don’t talk about cryptography or AES, it only explain how to encrypt and decrypt file using AES.
In .Net Rijndael encryption system (RijndaelManaged Class) is a part of cryptography (System.Security.Cryptography) . This class allows us to encrypt and decrypt file in .Net Environment.
Encryption Methodology
- Create new file stream using FileStream Class.
- Generate byte(UnicodeEncoding) of password(key) and initialization vector(IV)
- Creates a symmetric Encryptor object with Key (Password) and initialization vector (IV). RijndaelManaged algorithm supports key lengths of 128, 192, or 256 bits.
- Produced Crypto Stream using our original file with Encryptor object. Then write this crypto stream to our new file stream.
Encryption View
A simple form (encryptform) contain file and password fields are directly export data to controller method (EncryptFile )
- <form action="@Url.Action("EncryptFile","Home")" method="post" enctype="multipart/form-data" id="encryptform">
- <input type="file" name="encryptfile" id="encryptfile" />
- <input type="password" name="password" minlength="5" id="password" />
- <button type="submit">Upload</button>
- </form>
Encryption Controller
Required namespaces
- using System.IO;
- using System.Security.Cryptography;
- using System.Text;
The above three namespaces are required for encrypting and decrypting files. System.io use to create and read files, system.securtiy.cryptography for managing Rijndael streams for encrypting and decrypting files, finally system.Text for making key bytes from user password and initVector.
- public JsonResult EncryptFile(string password) {
- string InitVector = @ "qwertyui"; //16byte 1chr = 2byte(unicode)
- string baseUrl = "D://";
- ReturnData result = new ReturnData();
- result.success = false;
- try {
- if (Request.Files.Count > 0) {
- HttpPostedFileBase file = Request.Files[0];
- string filename = Path.GetFileName(file.FileName);
- string outputFile = Path.Combine(baseUrl, "encry_" + filename);
- UnicodeEncoding UE = new UnicodeEncoding();
- byte[] key = UE.GetBytes(password);
- byte[] IV = UE.GetBytes(InitVector);
- FileStream fsCrypt = new FileStream(outputFile, FileMode.Create);
- RijndaelManaged RMCrypto = new RijndaelManaged();
- ICryptoTransform encryptor = RMCrypto.CreateEncryptor(key, IV);
- CryptoStream cs = new CryptoStream(fsCrypt, encryptor, CryptoStreamMode.Write);
- int data;
- while ((data = file.InputStream.ReadByte()) != -1) {
- cs.WriteByte((byte) data);
- }
- file.InputStream.Close();
- cs.Close();
- fsCrypt.Close();
- result.success = true;
- result.url = "encry_" + filename;
- result.name = "encry_" + filename;
- }
- } catch (Exception e) {
- result.success = false;
- result.url = string.Empty;
- result.name = string.Empty;
- result.error = e.Message;
- }
- return Json(result, JsonRequestBehavior.AllowGet);
- }
Decryption Methodology
- Get encrypted file stream from HttpPostedFileBase file.
- Generate UnicodeEncoding byte of password(key) and initialization vector(IV)
- Creates a symmetric Decreptor object with the Key (Password) and initialization vector (IV).
- Produced Crypto Stream using our encrypted file with Decreptor object. Then write this decrypted files stream to new file. Finally the decrypted file is stored in Local Storage (D://)

Join the conversation! Your thoughts help the community grow.