How to extract a single decrypted page from multi page file using AES256 algorithm

  • Thread starter Thread starter Ankit Raman
  • Start date Start date
A

Ankit Raman

Guest
Hi All,


I have created a Winform test application for checking AES256 encryption/decryption working for large files.

I have a Single Text file which is containing multiple pages in it, it's size is 7.32GB and total pages was 1.8 millions.

I am trying to encrypt and decrypt this multi pages text file using AES256 algorithm.

Currently it is taking 3 mins in encrypting and almost 3 mins in decrypting. But i have below concern,

"so if we wanted to extract a single page from that document, would we need to completely decrypt it? or is there any way so that we could access pieces of the file from without incurring the overhead?"

I have used below code:

Encryption:

public static void Encrypt(string password, string inputFile, string outputFile)
{
//AES encryption on large files

//generate random salt
byte[] salt = GenerateRandomSalt();

//create output file name
FileStream fsCrypt = new FileStream(inputFile + ".aes", FileMode.Create);

//convert password string to byte arrray
byte[] passwordBytes = System.Text.Encoding.UTF8.GetBytes(password);

//Set AES symmetric encryption algorithm
AesManaged AES = new AesManaged();
AES.KeySize = 256;
AES.BlockSize = 128;
AES.Padding = PaddingMode.PKCS7;

//Why do I need to use the Rfc2898DeriveBytes class (in .NET) instead of directly using the password as a key or IV?
//"What it does is repeatedly hash the user password along with the salt." High iteration counts.
var key = new Rfc2898DeriveBytes(passwordBytes, salt, 50000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);

//Cipher modes: Which is the Best Cipher Mode and Padding Mode for AES Encryption?
AES.Mode = CipherMode.ECB;

// write salt to the begining of the output file, so in this case can be random every time
fsCrypt.Write(salt, 0, salt.Length);

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

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

//create a buffer (1mb) so only this amount will allocate in the memory and not the whole file
byte[] buffer = new byte[1048576];
int read;

try
{
while ((read = fsIn.Read(buffer, 0, buffer.Length)) > 0)
{
//Application.DoEvents(); // -> for responsive GUI, using Task will be better!
cs.Write(buffer, 0, read);
}

// Close up
fsIn.Close();
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
finally
{
cs.Close();
fsCrypt.Close();
}
//using (FileStream infs = File.OpenRead(inputfile))
//using (FileStream outfs = File.Create(outputfile))
// Encrypt(password, infs, outfs);
}

Decryption:

public static void Decrypt(string password, string inputFile, string outputFile)
{
byte[] passwordBytes = System.Text.Encoding.UTF8.GetBytes(password);
byte[] salt = new byte[32];

FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);
fsCrypt.Read(salt, 0, salt.Length);

AesManaged AES = new AesManaged();
AES.KeySize = 256;
AES.BlockSize = 128;
var key = new Rfc2898DeriveBytes(passwordBytes, salt, 50000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
AES.Padding = PaddingMode.PKCS7;
AES.Mode = CipherMode.ECB;

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

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

int read;
byte[] buffer = new byte[1048576];

try
{
while ((read = cs.Read(buffer, 0, buffer.Length)) > 0)
{
//Application.DoEvents();
fsOut.Write(buffer, 0, read);
}
}
catch (CryptographicException ex_CryptographicException)
{
Console.WriteLine("CryptographicException error: " + ex_CryptographicException.Message);
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}

try
{
cs.Close();
}
catch (Exception ex)
{
Console.WriteLine("Error by closing CryptoStream: " + ex.Message);
}
finally
{
fsOut.Close();
fsCrypt.Close();
}
}

Please suggest a way to achieve it, if possible.

Continue reading...
 
Back
Top