File Encryption using Rinjdael with Public and Private Key

The below is an example of how to encrypt and decrypt files using rinjdael with public and private keys for added security.

Step 1:-

First we need 4 Folders and 1 class:

Folder 1: “keys”

Folder 2: “UploadedFiles”

Folder 3: “EncryptedFiles”

Folder 4: “DecryptedFiles”

Class: “FileEncryption.cs”

Step 2:-

Class Imports in FileEncryption.cs

using System;
using System.Text;
using System.IO;
using System.Security.Cryptography;

Class Methods:

public byte[] AssymetricEncryt(byte[] data, string publicpath)
{

StreamReader reader = new StreamReader(publicpath);
string publicOnlyKeyXML = reader.ReadToEnd();

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(publicOnlyKeyXML);
reader.Close();

byte[] cipherbytes = rsa.Encrypt(data, false);
return cipherbytes;

}

public byte[] AssymetricDescrypt(byte[] data, string publicprivatepath)
{

StreamReader reader = new StreamReader(publicprivatepath);
string publicPrivateKeyXML = reader.ReadToEnd();

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(publicPrivateKeyXML);
reader.Close();

byte[] decryptedData= rsa.Decrypt(data, false);
return decryptedData;

}

public void EncryptFile(string inputFile, string outputFile, string publicpath)
{

try
{

string cryptFile = outputFile;
FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create);

RijndaelManaged RMCrypto = new RijndaelManaged();

byte[] ivEncrypt = AssymetricEncryt(RMCrypto.IV, publicpath);
fsCrypt.Write(ivEncrypt, 0, ivEncrypt.Length);

byte[] keyEncrypt = AssymetricEncryt(RMCrypto.Key, publicpath);
fsCrypt.Write(keyEncrypt, 0, keyEncrypt.Length);

CryptoStream cs = new CryptoStream(fsCrypt, RMCrypto.CreateEncryptor(),
CryptoStreamMode.Write);

FileStream fsIn = new FileStream(inputFile, FileMode.Open);

int data;
while ((data = fsIn.ReadByte()) != -1)
cs.WriteByte((byte)data);

fsIn.Close();
cs.Close();
fsCrypt.Close();

}
catch(Exception ex)
{

throw ex;

}

}

public void DecryptFile(string inputFile, string outputFile, string publicprivatepath)
{

try
{

FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);

RijndaelManaged RMCrypto = new RijndaelManaged();

byte[] iv = new byte[128];
byte[] key = new byte[128];

fsCrypt.Read(iv, 0, iv.Length);
fsCrypt.Read(key, 0, key.Length);

RMCrypto.IV = AssymetricDescrypt(iv, publicprivatepath);
RMCrypto.Key = AssymetricDescrypt(key, publicprivatepath);

CryptoStream cs = new CryptoStream(fsCrypt, RMCrypto.CreateDecryptor(),
CryptoStreamMode.Read);

FileStream fsOut = new FileStream(outputFile, FileMode.Create);

int data;
while ((data = cs.ReadByte()) != -1)
fsOut.WriteByte((byte)data);

fsOut.Close();
cs.Close();
fsCrypt.Close();

}
catch (Exception ex)
{

throw ex;

}

}

Step 3:-

Create a new ASPX Page:

<fieldset class=”form”>

<legend>Select Document to Upload and Encrypt:</legend>

<p>

<asp:FileUpload ID=”fu_file” runat=”server” />

</p>
<p>

<asp:Button ID=”btn_upload” runat=”server” Text=”Upload” onclick=”btn_upload_Click” />

</p>

</fieldset>

ASPX Code Behind

protected void btn_upload_Click(object sender, EventArgs e)
{

string userPublicKeyPath = “Keys/alicepublic.xml”; //name of the public key xml file
string userPairKeyPath = “Keys/alicepair.xml”  //name of the public and private key xml file

string inputURL = “../UploadedFiles/” + fu_file.FileName;  //Folder Created in step 1
string outputURL = “../EncryptedFiles/” + fu_file.FileName;  //Folder Created in step 1
string decURL = “../DecryptedFiles/” + fu_file.FileName;  //Folder Created in step 1

FileEncryption fe = new FileEncryption2();

// Encrypt the file.
fe.EncryptFile(Server.MapPath(inputURL), Server.MapPath(outputURL), Server.MapPath(userPublicKeyPath));
// Decrypt the file.
fe.DecryptFile(Server.MapPath(outputURL), Server.MapPath(decURL), Server.MapPath(userPairKeyPath));

}

If you also need help how to create the Public and Private Key read the article: Creating a Public and Private Key for Users

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s