示例#1
0
        public static bool DecryptFile(string path, EncryptionArgv encArgv)
        {
            try
            {
                FileStream fs      = new FileStream(path, FileMode.Open, FileAccess.Read);
                byte[]     rawData = new byte[fs.Length];
                fs.Read(rawData, 0, (int)fs.Length);
                fs.Close();
                fs.Dispose();

                Headers header        = new Headers();
                byte[]  decryptedData = null;
                byte[]  MD5Hash       = null;
                bool    ffcFile       = false;
                string  fileName      = Path.GetFileNameWithoutExtension(path);
                string  directoryName = Path.GetDirectoryName(path);
                if (encArgv.OutPath != "")
                {
                    directoryName = encArgv.OutPath;
                }

                // Check header
                if (header.CheckFFCFile(ref rawData))
                {
                    if (!(header.MatchPassword(encArgv.PrivatePassword, rawData)))
                    {
                        Console.WriteLine("\r\n    - Failed: Password do not match");
                        return(false);
                    }

                    if (!(header.MakeEncArgv(ref rawData, ref encArgv)))
                    {
                        Console.WriteLine("\r\n    - Failed: converting headers");
                        return(false);
                    }

                    ffcFile = true;
                    if (encArgv.NeedRestoreFilePath)
                    {
                        try
                        {
                            Directory.CreateDirectory(encArgv.EncFileInfo.FileDirectory);
                        }
                        catch { }

                        if (Directory.Exists(encArgv.EncFileInfo.FileDirectory))
                        {
                            directoryName = encArgv.EncFileInfo.FileDirectory;
                        }
                        else
                        {
                            Console.Write("\r\n    - Warning: Could not create directory: '" + encArgv.EncFileInfo.FileDirectory + "' - ");
                        }
                    }

                    if (encArgv.NeedRestoreInfo)
                    {
                        fileName = encArgv.EncFileInfo.FileName;
                    }
                    else
                    {
                        fileName = fileName + encArgv.EncFileInfo.FileExtension;
                    }

                    decryptedData = DecryptData(ref encArgv.EncFileInfo.FileData, encArgv.PrivatePassword, encArgv.AesInfo);

                    rawData = null;
                    header  = null;

                    MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
                    MD5Hash = md5.ComputeHash(decryptedData);
                    md5.Dispose();
                    md5 = null;

                    if (!((IStructuralEquatable)MD5Hash).Equals(encArgv.EncFileInfo.MD5FileHash, StructuralComparisons.StructuralEqualityComparer))
                    {
                        Console.WriteLine("\r\n    - Failed: Data do not match");
                        return(false);
                    }
                }
                else
                {
                    ffcFile       = false;
                    header        = null;
                    decryptedData = DecryptData(ref rawData, encArgv.PrivatePassword, encArgv.AesInfo);
                    rawData       = null;
                }

                string createPath = directoryName + "\\" + fileName;

                // Write decrypted data
                if (encArgv.ForceWrite & File.Exists(createPath))
                {
                    Console.Write("\r\n    - Warning: File Exists. Forced writing data... - ");
                    FileInfo file = new FileInfo(createPath);
                    if ((file.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                    {
                        file.Attributes = FileAttributes.Normal;
                    }
                }
                // Check file exists
                else if (File.Exists(createPath))
                {
                    createPath = IOObject.RenameFile(createPath);
                    Console.Write("\r\n    - Warning: File Exists. Renamed file: " + Path.GetFileName(createPath) + " - ");
                }

                using (FileStream fsb = new FileStream(createPath, FileMode.Create, FileAccess.Write))
                {
                    fsb.Write(decryptedData, 0, decryptedData.Length);
                }
                decryptedData = null;

                // Compressed Zip
                if (encArgv.EncFileInfo.IsCompressedZipFile)
                {
                    string folderPath = "";
                    using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile(createPath, Encoding.UTF8))
                    {
                        folderPath = Path.GetDirectoryName(createPath) + "\\" + zip.Comment;
                        Directory.CreateDirectory(folderPath);
                        if (encArgv.ForceWrite)
                        {
                            zip.ExtractAll(folderPath, Ionic.Zip.ExtractExistingFileAction.OverwriteSilently);
                        }
                        else
                        {
                            try
                            {
                                zip.ExtractAll(folderPath, Ionic.Zip.ExtractExistingFileAction.Throw);
                            }
                            catch
                            {
                                folderPath = IOObject.RenameFolder(folderPath);
                                zip.ExtractAll(folderPath, Ionic.Zip.ExtractExistingFileAction.OverwriteSilently);
                            }
                        }
                    }
                    File.Delete(createPath);
                }

                // Add file attributes
                if (ffcFile & encArgv.IsOriginalFile & encArgv.NeedRestoreInfo & !encArgv.EncFileInfo.IsCompressedZipFile)
                {
                    FileInfo fi = new FileInfo(createPath);
                    fi.CreationTime   = encArgv.EncFileInfo.CreationTime;
                    fi.LastWriteTime  = encArgv.EncFileInfo.LastWriteTime;
                    fi.LastAccessTime = encArgv.EncFileInfo.LastAccessTime;
                    if (encArgv.EncFileInfo.FileNotContentIndexed)
                    {
                        fi.Attributes |= FileAttributes.NotContentIndexed;
                    }
                    if (encArgv.EncFileInfo.FileOffline)
                    {
                        fi.Attributes |= FileAttributes.Offline;
                    }
                    if (encArgv.EncFileInfo.FileArchive)
                    {
                        fi.Attributes |= FileAttributes.Archive;
                    }
                    if (encArgv.EncFileInfo.FileHidden)
                    {
                        fi.Attributes |= FileAttributes.Hidden;
                    }
                    if (encArgv.EncFileInfo.FileSystem)
                    {
                        fi.Attributes |= FileAttributes.System;
                    }
                    if (encArgv.EncFileInfo.FileReadOnly)
                    {
                        fi.Attributes |= FileAttributes.ReadOnly;
                    }
                    fi = null;
                }

                // Delete file
                if (encArgv.NeedDeleteFile)
                {
                    IOObject.DeleteFileEx(path, encArgv.NeedFillDeleteFile);
                }
                return(true);
            }
            catch (Exception ex)
            {
                Console.WriteLine("\r\n    - Failed: " + ex.Message);
                return(false);
            }
        }