Зашифровать часть файла быстро - C#
Формулировка задачи:
Помогите пожалуйста, как зашифровать большой файл (больше гигабайта) с перезаписью его с помощью AES, возможно ли считать лиш чась файла и ее записать обратно по быстрому? или хотябы перезаписать файл
помогите что надо в код добавить чтобы перезаписывал допустим первые 128 килобайт файла и была возможность расшифровать потом вот пример кода что туда добавить?
Листинг программы
- private void AES_Encrypt2(string inputFile, string password)
- {
- //http://stackoverflow.com/questions/27645527/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 Rijndael symmetric encryption algorithm
- RijndaelManaged AES = new RijndaelManaged();
- AES.KeySize = 256;
- AES.BlockSize = 128;
- AES.Padding = PaddingMode.PKCS7;
- //http://stackoverflow.com/questions/2659214/why-do-i-need-to-use-the-rfc2898derivebytes-class-in-net-instead-of-directly
- //"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: [url]http://security.stackexchange.com/questions/52665/which-is-the-best-cipher-mode-and-padding-mode-for-aes-encryption[/url]
- AES.Mode = CipherMode.CBC; //cbf
- //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)
- {
- Debug.WriteLine("Error: " + ex.Message);
- }
- finally
- {
- cs.Close();
- fsCrypt.Close();
- }
- }
- private void AES_Decrypt2(string inputFile, string password)
- {
- //todo:
- // - create error message on wrong password
- // - on cancel: close and delete file
- // - on wrong password: close and delete file!
- // - create a better filen name
- // - could be check md5 hash on the files but it make this slow
- 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);
- RijndaelManaged AES = new RijndaelManaged();
- 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.CBC; //CFB
- CryptoStream cs = new CryptoStream(fsCrypt, AES.CreateDecryptor(), CryptoStreamMode.Read);
- FileStream fsOut = new FileStream(inputFile + ".decrypted", 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 (System.Security.Cryptography.CryptographicException ex_CryptographicException)
- {
- Debug.WriteLine("CryptographicException error: " + ex_CryptographicException.Message);
- }
- catch (Exception ex)
- {
- Debug.WriteLine("Error: " + ex.Message);
- }
- try
- {
- cs.Close();
- }
- catch (Exception ex)
- {
- Debug.WriteLine("Error by closing CryptoStream: " + ex.Message);
- }
- finally
- {
- fsOut.Close();
- fsCrypt.Close();
- }
- }
- public static byte[] GenerateRandomSalt()
- {
- //Source: [url]http://www.dotnetperls.com/rngcryptoserviceprovider[/url]
- byte[] data = new byte[32];
- using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
- {
- // Ten iterations.
- for (int i = 0; i < 10; i++)
- {
- // Fill buffer.
- rng.GetBytes(data);
- }
- }
- return data;
- }
Решение задачи: «Зашифровать часть файла быстро»
textual
Листинг программы
- byte[] buffer = new byte[1048576];
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д