Hi Code project mates,
I have a serious problem with encryption and decryption. What I have is a folder which can be in varied sizes but can have an amount 100 GB or so.
My project need:
The need is when I log off the system the windows service I have created should encrypt the folder(which is the folder with 100 GB data). And when I log in again it should decrypt it and make the folder usable.
So here comes my problem:
I am right now have a method which encrypts and decrypts files inside a folder in a loop format. So just imagine if I have a 100 GB or above in a folder while we log off or log in. The system may hang with such large amount of data.
What I want from you guys:
I am not asking for complete code from you guys. I need just an help.
Actually I want some code samples or something which helps me get rid of the problem. Also I am searching for the encrypting the folder on the whole instead of individual files inside.
I am looking for making this software similar to BITLOCKER. My service should handle the large files with ease.
What I have tried:
I have tried the below code:
protected void enCrypt(string filePath, string passWord)
{
byte[] filestobeEncrypted = File.ReadAllBytes(filePath);
byte[] passwordBytes = Encoding.UTF8.GetBytes(passWord);
passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
byte[] bytesEncrypted = AES_Encrypt(filestobeEncrypted, passwordBytes);
File.WriteAllBytes(filePath, bytesEncrypted);
}
protected void deCrypt(string filePath, string passWord)
{
byte[] filestobeDecrypted = File.ReadAllBytes(filePath);
byte[] passwordBytes = Encoding.UTF8.GetBytes(passWord);
passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
byte[] bytesDecrypted = AES_Decrypt(filestobeDecrypted, passwordBytes);
File.WriteAllBytes(filePath, bytesDecrypted);
}
public static byte[] AES_Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes)
{
byte[] encryptedBytes = null;
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
using (MemoryStream ms = new MemoryStream())
{
using (RijndaelManaged AES = new RijndaelManaged())
{
AES.KeySize = 256;
AES.BlockSize = 128;
var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
AES.Mode = CipherMode.CBC;
using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);
cs.Close();
}
encryptedBytes = ms.ToArray();
}
}
return encryptedBytes;
}
public static byte[] AES_Decrypt(byte[] bytesToBeDecrypted, byte[] passwordBytes)
{
byte[] decryptedBytes = null;
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
using (MemoryStream ms = new MemoryStream())
{
using (RijndaelManaged AES = new RijndaelManaged())
{
AES.KeySize = 256;
AES.BlockSize = 128;
var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
AES.Mode = CipherMode.CBC;
using (var cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
cs.Close();
}
decryptedBytes = ms.ToArray();
}
}
return decryptedBytes;
}