private static void _DecryptPreprocess(string inputFile) { string outputFile = inputFile + ".tmp"; int numberOfBytes = 16; byte[] bytes = new byte[numberOfBytes]; //get GUID from file using (var fs = new FileStream(inputFile, FileMode.Open, FileAccess.Read)) { fs.Read(bytes, 0, bytes.Length); } // var buffer2 = File.ReadAllBytes(inputFile); Guid guid = new Guid(bytes); string GUID = guid.ToString(); //create new file without GUID char[] delimiterChars = { '.' }; string[] newFile = outputFile.Split(delimiterChars); string newFileFullPathName = String.Join(".", newFile.Take(newFile.Length - 2)); using (var fs = new FileStream(inputFile, FileMode.Open, FileAccess.Read)) { byte[] fullfile = new byte[fs.Length]; int toRead = (int)fs.Length - 1, bytesRead; fs.Seek(16, SeekOrigin.Begin); while (toRead > 0 && (bytesRead = fs.Read(fullfile, 0, toRead)) > 0) { toRead -= bytesRead; numberOfBytes += bytesRead; } using (var newFS = new FileStream(outputFile, FileMode.Create, FileAccess.Write)) { newFS.Write(fullfile, 0, fullfile.Length); } } //get IV and key from SQLite DB SecureboxKey el = DatabaseManagement.GetSecureboxKey(GUID); if (el != null && el.IV != null && el.IV.Length > 0) { Securebox._DecryptFile(outputFile, newFileFullPathName, el); } Securebox.DeleteCurrentFile(outputFile); }
private static void _EncryptFile(string inputFile, string outputFile, Guid GUID) { try { using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider()) { /* This is for demostrating purposes only. * Ideally you will want the IV key to be different from your key and you should always generate a new one for each encryption in other to achieve maximum security*/ byte[] IV = GUID.ToByteArray(); byte[] key = GUID.ToByteArray(); DatabaseManagement.InsertKey(GUID.ToString(), IV, key); using (FileStream fsCrypt = new FileStream(outputFile, FileMode.Create)) { using (ICryptoTransform encryptor = aes.CreateEncryptor(key, IV)) { using (CryptoStream cs = new CryptoStream(fsCrypt, encryptor, CryptoStreamMode.Write)) { using (FileStream fsIn = new FileStream(inputFile, FileMode.Open)) { int data; while ((data = fsIn.ReadByte()) != -1) { cs.WriteByte((byte)data); } } } } } } } catch (Exception ex) { Debug.WriteLine(ex.Message); } Securebox.DeleteCurrentFile(inputFile); }
private static Boolean _PostProcessEncryptFile(string inputFile, string outputFile, Guid GUID) { Boolean isCancelEncryptionProcess = false; if (File.Exists(outputFile)) { DialogResult dialogResult = System.Windows.Forms.MessageBox.Show("Would you like to overwrite an existing file?\nSelecting 'No' will append the current file name with a - and a numeral for the current file.\nSelecting 'Cancel' will skip all remaining files as well as this file", "Overwrite confirmation", MessageBoxButtons.YesNoCancel); if (dialogResult == DialogResult.Yes) { Securebox.DeleteCurrentFile(outputFile); Securebox._PostProcessEncryptFileMethod(inputFile, outputFile, GUID); } isCancelEncryptionProcess = dialogResult == DialogResult.Cancel; } else { Securebox._PostProcessEncryptFileMethod(inputFile, outputFile, GUID); } Securebox.DeleteCurrentFile(inputFile); return(isCancelEncryptionProcess); }