/// <summary> /// Compress (TAR/BZip2) an unencrypted FAES File. /// </summary> /// <param name="unencryptedFile">Unencrypted FAES File</param> /// <returns>Path of the unencrypted, TAR/BZip2 compressed file</returns> public string CompressFAESFile(FAES_File unencryptedFile) { FileAES_IntUtilities.CreateEncryptionFilePath(unencryptedFile, "TAR", out string tempRawPath, out _, out string tempOutputPath); TarWriterOptions wo = new TarWriterOptions(CompressionType.BZip2, true); using (Stream stream = File.OpenWrite(tempOutputPath)) using (var writer = new TarWriter(stream, wo)) { writer.WriteAll(tempRawPath, "*", SearchOption.AllDirectories); } return(tempOutputPath); }
/// <summary> /// Compress (LZMA) an unencrypted FAES File. /// </summary> /// <param name="unencryptedFile">Unencrypted FAES File</param> /// <returns>Path of the unencrypted, LZMA compressed file</returns> public string CompressFAESFile(FAES_File unencryptedFile) { FileAES_IntUtilities.CreateEncryptionFilePath(unencryptedFile, "LZMA", out string tempRawPath, out _, out string tempOutputPath); WriterOptions wo = new WriterOptions(CompressionType.LZMA); using (Stream stream = File.OpenWrite(tempOutputPath)) using (var writer = WriterFactory.Open(stream, ArchiveType.Zip, wo)) { writer.WriteAll(tempRawPath, "*", SearchOption.AllDirectories); } return(tempOutputPath); }
/// <summary> /// Compress (ZIP) an unencrypted FAES File. /// </summary> /// <param name="unencryptedFile">Unencrypted FAES File</param> /// <returns>Path of the unencrypted, ZIP compressed file</returns> public string CompressFAESFile(FAES_File unencryptedFile) { FileAES_IntUtilities.CreateEncryptionFilePath(unencryptedFile, "ZIP", out string tempRawPath, out _, out string tempOutputPath); ZipWriterOptions wo = new ZipWriterOptions(CompressionType.Deflate) { DeflateCompressionLevel = _compressLevel }; using (Stream stream = File.OpenWrite(tempOutputPath)) using (var writer = new ZipWriter(stream, wo)) { writer.WriteAll(tempRawPath, "*", SearchOption.AllDirectories); } return(tempOutputPath); }
/// <summary> /// Compress (LGYZIP) an unencrypted FAES File. /// </summary> /// <param name="unencryptedFile">Unencrypted FAES File</param> /// <returns>Path of the unencrypted, LGYZIP compressed file</returns> public string CompressFAESFile(FAES_File unencryptedFile) { FileAES_IntUtilities.CreateEncryptionFilePath(unencryptedFile, "LGYZIP", out string tempRawPath, out _, out string tempOutputPath); if (unencryptedFile.IsFile()) { using (ZipArchive zip = ZipFile.Open(tempOutputPath, ZipArchiveMode.Create)) { zip.CreateEntryFromFile(unencryptedFile.GetPath(), unencryptedFile.GetFileName()); zip.Dispose(); } } else { FileAES_IntUtilities.DirectoryCopy(unencryptedFile.GetPath(), tempRawPath); ZipFile.CreateFromDirectory(tempRawPath, tempOutputPath); } return(tempOutputPath); }
/// <summary> /// Decrypts the selected file using the given password /// </summary> /// <param name="inputFile">Encrypted File</param> /// <param name="password">Password to decrypt the file</param> /// <param name="percentComplete">Percent of completion</param> /// <returns>If the decryption was successful</returns> internal bool Decrypt(string inputFile, string password, ref decimal percentComplete) { byte[] passwordBytes = Encoding.UTF8.GetBytes(password); FileStream fsCrypt = new FileStream(inputFile, FileMode.Open); fsCrypt = DecryptModeHandler(fsCrypt, out byte[] hash, out byte[] salt, out byte[] faesCBCMode, out byte[] faesMetaData, out var cipher); const int keySize = 256; const int blockSize = 128; Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(passwordBytes, salt, 50000); RijndaelManaged AES = new RijndaelManaged { KeySize = keySize, BlockSize = blockSize, Key = key.GetBytes(keySize / 8), IV = key.GetBytes(blockSize / 8), Padding = PaddingMode.PKCS7, Mode = cipher }; try { CryptoStream cs = new CryptoStream(fsCrypt, AES.CreateDecryptor(), CryptoStreamMode.Read); string outputName = Path.ChangeExtension(inputFile, FileAES_Utilities.ExtentionUFAES); try { FileStream fsOut = new FileStream(outputName, FileMode.Create); File.SetAttributes(outputName, FileAttributes.Hidden); byte[] buffer = new byte[FileAES_Utilities.GetCryptoStreamBuffer()]; long expectedComplete = fsCrypt.Length + hash.Length + salt.Length + faesCBCMode.Length + faesMetaData.Length + AES.KeySize + AES.BlockSize; try { int read; Logging.Log("Beginning writing decrypted data...", Severity.DEBUG); while ((read = cs.Read(buffer, 0, buffer.Length)) > 0) { try { percentComplete = Math.Ceiling((decimal)((Convert.ToDouble(fsOut.Length) / Convert.ToDouble(expectedComplete)) * 100)); if (percentComplete > 100) { percentComplete = 100; } } catch { Logging.Log("Percentage completion calculation failed!", Severity.WARN); } fsOut.Write(buffer, 0, read); } Logging.Log("Finished writing decrypted data.", Severity.DEBUG); } catch { fsOut.Close(); } cs.Close(); fsOut.Close(); fsCrypt.Close(); if (Checksums.ConvertHashToString(hash) != Checksums.ConvertHashToString(Checksums.GetSHA1(outputName))) { Logging.Log("Invalid Checksum detected! Assuming password is incorrect.", Severity.DEBUG); FileAES_IntUtilities.SafeDeleteFile(outputName); return(false); } Logging.Log("Valid Checksum detected!", Severity.DEBUG); return(true); } catch { cs.Close(); fsCrypt.Close(); return(false); } } catch (CryptographicException) { fsCrypt.Close(); return(false); } }