FAES/AES/Crypt.cs
/// <summary>
/// Decrypts the selected file using the given password
/// </summary>
/// <param name="faesMetaData">Formatted Metadata used at the start of a file</param>
/// <param name="inputFilePath">File path for encrypted file</param>
/// <param name="outputFilePath">File path for unencrypted file</param>
/// <param name="encryptionPassword">Encryption Password</param>
/// <param name="percentComplete">Percent completion of the encryption process</param>
/// <returns>If the decryption was successful</returns>
Decrypts the selected file using the given password
Decrypts the selected file using the given password
internal bool Decrypt(MetaData faesMetaData, string inputFilePath, string outputFilePath, string encryptionPassword, ref decimal percentComplete) 
        {
            byte[] metaData = new byte[faesMetaData.GetLength()];
            byte[] salt = new byte[32];
            byte[] passwordBytes = Encoding.UTF8.GetBytes(encryptionPassword);
            FileStream inputDataStream = new FileStream(inputFilePath, FileMode.Open);
            inputDataStream.Read(metaData, 0, faesMetaData.GetLength());
            inputDataStream.Read(salt, 0, salt.Length);
            Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(passwordBytes, salt, _keyIterations);
            RijndaelManaged AES = new RijndaelManaged
            {
                KeySize = _keySize,
                BlockSize = _blockSize,
                Key = key.GetBytes(_keySize / 8),
                IV = key.GetBytes(_blockSize / 8),
                Padding = _paddingMode,
                Mode = _cipher
            };
            try
            {
                CryptoStream crypto = new CryptoStream(inputDataStream, AES.CreateDecryptor(), CryptoStreamMode.Read);
                FileStream outputDataStream = new FileStream(outputFilePath, FileMode.Create);
                try
                {
                    byte[] buffer = new byte[FileAES_Utilities.GetCryptoStreamBuffer()];
                    long expectedComplete = salt.Length + AES.KeySize + AES.BlockSize + inputDataStream.Length;
                    try
                    {
                        Logging.Log("Beginning writing decrypted data...");
                        int read;
                        while ((read = crypto.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
                            {
                            }
                            outputDataStream.Write(buffer, 0, read);
                        }
                        Logging.Log("Finished writing decrypted data.");
                        percentComplete = 100;
                    }
                    catch
                    {
                        outputDataStream.Close();
                    }
                    crypto.Close();
                    outputDataStream.Close();
                    inputDataStream.Close();
                    bool doesHashMatch = false;
                    switch (faesMetaData.GetHashType())
                    {
                        case Checksums.ChecksumType.SHA1:
                            doesHashMatch = Checksums.CompareHash(faesMetaData.GetOrigHash(), Checksums.GetSHA1(outputFilePath));
                            break;
                        case Checksums.ChecksumType.SHA256:
                            doesHashMatch = Checksums.CompareHash(faesMetaData.GetOrigHash(), Checksums.GetSHA256(outputFilePath));
                            break;
                        case Checksums.ChecksumType.SHA384:
                            doesHashMatch = Checksums.CompareHash(faesMetaData.GetOrigHash(), Checksums.GetSHA384(outputFilePath));
                            break;
                        case Checksums.ChecksumType.SHA512:
                            doesHashMatch = Checksums.CompareHash(faesMetaData.GetOrigHash(), Checksums.GetSHA512(outputFilePath));
                            break;
                        default:
                            Logging.Log($"{faesMetaData.GetHashType()} is an unknown checksum type! Ensure you are using an updated version of FAES!", Severity.WARN);
                            break;
                    }
                    if (!doesHashMatch)
                    {
                        Logging.Log("Invalid Checksum detected! Assuming password is incorrect.");
                        return false;
                    }
                    Logging.Log("Valid Checksum detected!");
                    return true;
                }
                catch
                {
                    crypto.Close();
                    inputDataStream.Close();
                    outputDataStream.Close();
                    return false;
                }
            }
            catch (CryptographicException)
            {
                inputDataStream.Close();
                return false;
            }
        }
internal bool Decrypt ( MetaData faesMetaData , string inputFilePath , string outputFilePath , string encryptionPassword , ref decimal percentComplete ) { byte [ ] metaData = new byte [ faesMetaData . GetLength ( ) ] ; byte [ ] salt = new byte [ 32 ] ; byte [ ] passwordBytes = Encoding . UTF8 . GetBytes ( encryptionPassword ) ; FileStream inputDataStream = new FileStream ( inputFilePath , FileMode . Open ) ; inputDataStream . Read ( metaData , 0 , faesMetaData . GetLength ( ) ) ; inputDataStream . Read ( salt , 0 , salt . Length ) ; Rfc2898DeriveBytes key = new Rfc2898DeriveBytes ( passwordBytes , salt , _keyIterations ) ; RijndaelManaged AES = new RijndaelManaged { KeySize = _keySize , BlockSize = _blockSize , Key = key . GetBytes ( _keySize / 8 ) , IV = key . GetBytes ( _blockSize / 8 ) , Padding = _paddingMode , Mode = _cipher } ; try { CryptoStream crypto = new CryptoStream ( inputDataStream , AES . CreateDecryptor ( ) , CryptoStreamMode . Read ) ; FileStream outputDataStream = new FileStream ( outputFilePath , FileMode . Create ) ; try { byte [ ] buffer = new byte [ FileAES_Utilities . GetCryptoStreamBuffer ( ) ] ; long expectedComplete = salt . Length + AES . KeySize + AES . BlockSize + inputDataStream . Length ; try { Logging . Log ( " " ) ; int read ; while ( ( read = crypto . 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 { } outputDataStream . Write ( buffer , 0 , read ) ; } Logging . Log ( " " ) ; percentComplete = 100 ; } catch { outputDataStream . Close ( ) ; } crypto . Close ( ) ; outputDataStream . Close ( ) ; inputDataStream . Close ( ) ; bool doesHashMatch = false ; switch ( faesMetaData . GetHashType ( ) ) { case Checksums . ChecksumType . SHA1 : doesHashMatch = Checksums . CompareHash ( faesMetaData . GetOrigHash ( ) , Checksums . GetSHA1 ( outputFilePath ) ) ; break ; case Checksums . ChecksumType . SHA256 : doesHashMatch = Checksums . CompareHash ( faesMetaData . GetOrigHash ( ) , Checksums . GetSHA256 ( outputFilePath ) ) ; break ; case Checksums . ChecksumType . SHA384 : doesHashMatch = Checksums . CompareHash ( faesMetaData . GetOrigHash ( ) , Checksums . GetSHA384 ( outputFilePath ) ) ; break ; case Checksums . ChecksumType . SHA512 : doesHashMatch = Checksums . CompareHash ( faesMetaData . GetOrigHash ( ) , Checksums . GetSHA512 ( outputFilePath ) ) ; break ; default : Logging . Log ( $" { faesMetaData . GetHashType ( ) }  is an unknown checksum type! Ensure you are using an updated version of FAES! " , Severity . WARN ) ; break ; } if ( ! doesHashMatch ) { Logging . Log ( " " ) ; return false ; } Logging . Log ( " " ) ; return true ; } catch { crypto . Close ( ) ; inputDataStream . Close ( ) ; outputDataStream . Close ( ) ; return false ; } } catch ( CryptographicException ) { inputDataStream . Close ( ) ; return false ; } }
Decrypts the selected file using the given password
Decrypts the selected file using the given password
// TODO: Diagnose Decryption Progression not updating till finished // ignored
If the decryption was successful
If the decryption was successful
faesMetaData
MetaData
Formatted Metadata used at the start of a file
Formatted Metadata used at the start of a file
inputFilePath
string
File path for encrypted file
File path for encrypted file
outputFilePath
string
File path for unencrypted file
File path for unencrypted file
encryptionPassword
string
None
decimal
ref
percentComplete
Percent completion of the encryption process
Percent completion of the encryption process