justinooo

AES File Encryption w/ Percentage

Aug 20th, 2016
976
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.         private static void encryptFile(string inputFile, string outputFile, string password)
  2.         {
  3.             byte[] salt = GenerateRandomSalt();
  4.             FileStream fsCrypt = new FileStream(outputFile, FileMode.Create);
  5.             byte[] passwordBytes = System.Text.Encoding.UTF8.GetBytes(password);
  6.             RijndaelManaged AES = new RijndaelManaged();
  7.             AES.KeySize = 256;
  8.             AES.BlockSize = 128;
  9.             AES.Padding = PaddingMode.PKCS7;
  10.             var key = new Rfc2898DeriveBytes(passwordBytes, salt, 50000);
  11.             AES.Key = key.GetBytes(AES.KeySize / 8);
  12.             AES.IV = key.GetBytes(AES.BlockSize / 8);
  13.             AES.Mode = CipherMode.CFB;
  14.             fsCrypt.Write(salt, 0, salt.Length);
  15.             CryptoStream cs = new CryptoStream(fsCrypt, AES.CreateEncryptor(), CryptoStreamMode.Write);
  16.             FileStream fsIn = new FileStream(inputFile, FileMode.Open);
  17.             byte[] buffer = new byte[1048576];
  18.             int read;
  19.             int pastP = 0;
  20.             while ((read = fsIn.Read(buffer, 0, buffer.Length)) > 0)
  21.             {
  22.                 cs.Write(buffer, 0, read);
  23.                 var percentComplete = (float)fsIn.Position * 100 / fsIn.Length;
  24.                 int percentInt = Convert.ToInt32(percentComplete);
  25.                 if (pastP != percentInt)
  26.                 {
  27.                     Console.WriteLine(percentInt);
  28.                 }
  29.                 pastP = percentInt;
  30.             }
  31.             fsIn.Close();
  32.             cs.Close();
  33.             fsCrypt.Close();
  34.         }
  35.         private static void decryptFile(string inputFile, string outputFile, string password)
  36.         {
  37.             byte[] passwordBytes = System.Text.Encoding.UTF8.GetBytes(password);
  38.             byte[] salt = new byte[32];
  39.             FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);
  40.             fsCrypt.Read(salt, 0, salt.Length);
  41.             RijndaelManaged AES = new RijndaelManaged();
  42.             AES.KeySize = 256;
  43.             AES.BlockSize = 128;
  44.             var key = new Rfc2898DeriveBytes(passwordBytes, salt, 50000);
  45.             AES.Key = key.GetBytes(AES.KeySize / 8);
  46.             AES.IV = key.GetBytes(AES.BlockSize / 8);
  47.             AES.Padding = PaddingMode.PKCS7;
  48.             AES.Mode = CipherMode.CFB;
  49.             CryptoStream cs = new CryptoStream(fsCrypt, AES.CreateDecryptor(), CryptoStreamMode.Read);
  50.             FileStream fsOut = new FileStream(outputFile, FileMode.Create);
  51.             int read;
  52.             byte[] buffer = new byte[1048576];
  53.             int pastP = 0;
  54.             while ((read = cs.Read(buffer, 0, buffer.Length)) > 0)
  55.             {
  56.                 fsOut.Write(buffer, 0, read);
  57.                 var percentComplete = (float)fsOut.Position * 100 / fsCrypt.Length;
  58.                 int percentInt = Convert.ToInt32(percentComplete);
  59.                 if (pastP != percentInt)
  60.                 {
  61.                     Console.WriteLine(percentInt);
  62.                 }
  63.                 pastP = percentInt;
  64.             }          
  65.             fsOut.Close();
  66.             fsCrypt.Close();
  67.         }
  68.        
  69.         public static byte[] GenerateRandomSalt()
  70.         {
  71.             byte[] data = new byte[32];
  72.             using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
  73.             {
  74.                 for (int i = 0; i < 10; i++)
  75.                 {
  76.                     rng.GetBytes(data);
  77.                 }
  78.             }
  79.             return data;
  80.         }
RAW Paste Data