FAES/AES/Crypt.cs
/// <summary>
/// Encrypts the selected file using the given password
/// </summary>
/// <param name="metaData">Formatted Metadata used at the start of a file</param>
/// <param name="inputFilePath">File path for unencrypted file</param>
/// <param name="outputFilePath">File path for encrypted file</param>
/// <param name="encryptionPassword">Encryption Password</param>
/// <param name="percentComplete">Percent completion of the encryption process</param>
/// <returns>If the encryption was successful</returns>
Encrypts the selected file using the given password
Encrypts the selected file using the given password
internal bool Encrypt(byte[] metaData, string inputFilePath, string outputFilePath, string encryptionPassword, ref decimal percentComplete) 
        {
            byte[] salt = _specifiedSalt ?? CryptUtils.GenerateRandomSalt();
            byte[] passwordBytes = Encoding.UTF8.GetBytes(encryptionPassword);
            Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(passwordBytes, salt, _keyIterations);
            RijndaelManaged AES = new RijndaelManaged
            {
                KeySize = _keySize,
                BlockSize = _blockSize,
                Padding = _paddingMode,
                Key = key.GetBytes(_keySize / 8),
                IV = key.GetBytes(_blockSize / 8),
                Mode = _cipher
            };
            FileStream outputDataStream = new FileStream(outputFilePath, FileMode.Create);
            outputDataStream.Write(metaData, 0, metaData.Length);
            outputDataStream.Write(salt, 0, salt.Length);
            CryptoStream crypto = new CryptoStream(outputDataStream, AES.CreateEncryptor(), CryptoStreamMode.Write);
            FileStream inputDataStream = new FileStream(inputFilePath, FileMode.Open);
            byte[] buffer = new byte[FileAES_Utilities.GetCryptoStreamBuffer()];
            int read;
            long expectedComplete = metaData.Length + AES.KeySize + AES.BlockSize + inputDataStream.Length;
            Logging.Log("Beginning writing encrypted data...", Severity.DEBUG);
            while ((read = inputDataStream.Read(buffer, 0, buffer.Length)) > 0)
            {
                try
                {
                    percentComplete = Math.Ceiling((decimal)((Convert.ToDouble(outputDataStream.Length) / Convert.ToDouble(expectedComplete)) * 100));
                    if (percentComplete > 100) percentComplete = 100;
                }
                catch
                {
                }
                crypto.Write(buffer, 0, read);
            }
            Logging.Log("Finished writing encrypted data.", Severity.DEBUG);
            percentComplete = 100;
            inputDataStream.Close();
            crypto.Close();
            outputDataStream.Close();
            return true;
        }
internal bool Encrypt ( byte [ ] metaData , string inputFilePath , string outputFilePath , string encryptionPassword , ref decimal percentComplete ) { byte [ ] salt = _specifiedSalt ?? CryptUtils . GenerateRandomSalt ( ) ; byte [ ] passwordBytes = Encoding . UTF8 . GetBytes ( encryptionPassword ) ; Rfc2898DeriveBytes key = new Rfc2898DeriveBytes ( passwordBytes , salt , _keyIterations ) ; RijndaelManaged AES = new RijndaelManaged { KeySize = _keySize , BlockSize = _blockSize , Padding = _paddingMode , Key = key . GetBytes ( _keySize / 8 ) , IV = key . GetBytes ( _blockSize / 8 ) , Mode = _cipher } ; FileStream outputDataStream = new FileStream ( outputFilePath , FileMode . Create ) ; outputDataStream . Write ( metaData , 0 , metaData . Length ) ; outputDataStream . Write ( salt , 0 , salt . Length ) ; CryptoStream crypto = new CryptoStream ( outputDataStream , AES . CreateEncryptor ( ) , CryptoStreamMode . Write ) ; FileStream inputDataStream = new FileStream ( inputFilePath , FileMode . Open ) ; byte [ ] buffer = new byte [ FileAES_Utilities . GetCryptoStreamBuffer ( ) ] ; int read ; long expectedComplete = metaData . Length + AES . KeySize + AES . BlockSize + inputDataStream . Length ; Logging . Log ( " " , Severity . DEBUG ) ; while ( ( read = inputDataStream . Read ( buffer , 0 , buffer . Length ) ) > 0 ) { try { percentComplete = Math . Ceiling ( ( decimal ) ( ( Convert . ToDouble ( outputDataStream . Length ) / Convert . ToDouble ( expectedComplete ) ) * 100 ) ) ; if ( percentComplete > 100 ) percentComplete = 100 ; } catch { } crypto . Write ( buffer , 0 , read ) ; } Logging . Log ( " " , Severity . DEBUG ) ; percentComplete = 100 ; inputDataStream . Close ( ) ; crypto . Close ( ) ; outputDataStream . Close ( ) ; return true ; }
Encrypts the selected file using the given password
Encrypts the selected file using the given password
// TODO: Diagnose Encryption Progression not updating till finished // ignored
If the encryption was successful
If the encryption was successful
metaData
byte[]
Formatted Metadata used at the start of a file
Formatted Metadata used at the start of a file
inputFilePath
string
File path for unencrypted file
File path for unencrypted file
outputFilePath
string
File path for encrypted file
File path for encrypted file
encryptionPassword
string
None
decimal
ref
percentComplete
Percent completion of the encryption process
Percent completion of the encryption process