static void Main(string[] args) { // init class EncryptDecrypt encrypt = new Encrypt(); EncryptDecrypt decrypt = new Decrypt(); ValidateStart validate = new ValidateStart(); Method methods = new Method(); // create thread Thread thread; // origin input validation validate.ValidateOrigin(); // if input is enc(encrypt) or dec(decrypt) if (validate.GetUserInput().ToLower().Equals("enc")) { thread = new Thread(() => encrypt.EncryptData(validate.GetPath())); } else { thread = new Thread(() => decrypt.DecryptData(validate.GetPath())); } // start thread thread.Start(); }
// encrypt specified file public override void EncryptData(string path) { // init classes Messages messages = new Messages(); EncryptDecrypt decrypt = new Decrypt(); Validation validation = new Validation(); Method methods = new Method(); // variables byte[] secretKey = null; byte[] IV = null;; string elapsedTime, directory, fileName, filePath, lastItem; bool firstRun = true, error = false, lastRun = false; // new list List <string> fileList = new List <string>(); // add files to list if directory, else add only the path itself if (validation.ValidateDirectory(path)) { fileList = new List <string>(methods.GetFiles(path)); } else { fileList.Add(path); } // get last item in fileList lastItem = fileList[(fileList.Count - 1)]; // new stopwatch Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); try { // encrypt all files in fileList foreach (string file in fileList) { // set and reset variables byte[] encryptedBytes = null, fileAsBytes = null, extensionAsBytes = null; IEnumerable <byte> fullBytes = null, plainBytes = null; string extension = null; // if last iteration if (file.Equals(lastItem)) { lastRun = true; } // read file fileAsBytes = File.ReadAllBytes(file); // extension to be appended to file extension = Path.GetExtension(file); // append extension to file extensionAsBytes = Encoding.ASCII.GetBytes(extension); plainBytes = fileAsBytes.Concat(extensionAsBytes); fileAsBytes = plainBytes.ToArray(); // create rijndael object Rijndael rijndael = Rijndael.Create(); // generate only one key/IV for the current instance if (firstRun) { secretKey = Encoding.ASCII.GetBytes(methods.generateKey(16)); IV = Encoding.ASCII.GetBytes(methods.generateKey(16)); firstRun = false; Console.WriteLine(Environment.NewLine); } // set values rijndael.Key = secretKey; rijndael.Mode = CipherMode.CBC; rijndael.Padding = PaddingMode.PKCS7; rijndael.IV = IV; // open streams with data MemoryStream memoryStream = new MemoryStream(); CryptoStream cryptoStream = new CryptoStream(memoryStream, rijndael.CreateEncryptor(), CryptoStreamMode.Write); // progress notification Console.SetCursorPosition(0, Console.CursorTop - 1); methods.ClearCurrentConsoleLine(); if (!lastRun) { messages.ProcessMessage(file); } else { messages.ProcessCompleteMessage(); } // encrypt cryptoStream.Write(fileAsBytes, 0, fileAsBytes.Length); cryptoStream.Close(); // append IV to content fullBytes = memoryStream.ToArray().Concat(IV).ToArray(); encryptedBytes = fullBytes.ToArray(); memoryStream.Close(); // write encrypted content to file System.IO.File.WriteAllBytes(file, encryptedBytes); // progress notification Console.SetCursorPosition(0, Console.CursorTop + 1); methods.ClearCurrentConsoleLine(); if (!lastRun) { messages.EncryptProgressMessage(file); } else { messages.EncryptionCompleteMessage(); } // set file extension to .rip File.Move(file, Path.ChangeExtension(file, ".rip")); // clear resources rijndael.Dispose(); } } catch (Exception E) { error = true; if (E is UnauthorizedAccessException) { messages.AuthMessage(); } else if (E is CryptographicException) { messages.CryptoError(); } else { messages.UnexpectedError(); } } // print if no error was encountered if (!error) { messages.FileToRipMessage(); } // stop stopwatch stopWatch.Stop(); // print elapsed time TimeSpan ts = stopWatch.Elapsed; elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10); messages.TimeMessage(elapsedTime); // catch file/directory creation/write errors try { // create directory if it does not already exist then add file containing key directory = Path.GetPathRoot(Environment.GetFolderPath(Environment.SpecialFolder.System)) + "autocryptKeyStorage"; if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } fileName = DateTime.Now.ToString("HH_mm_ss") + "_keyKeeper.key"; filePath = directory + "\\" + fileName; File.WriteAllText(Path.Combine(directory, fileName), methods.WarpKey(Encoding.ASCII.GetString(secretKey))); // print if no error was encountered if (!error) { messages.SuccessMessage(); } // print if key is not empty or null if (!Encoding.ASCII.GetString(secretKey).Equals("") || !Encoding.ASCII.GetString(secretKey).Equals(null)) { // provide user with decryption key messages.KeyMessage(methods.WarpKey(Encoding.ASCII.GetString(secretKey)), filePath); } } catch (Exception E) { // if matrix is null or if unexpected error if (E is ArgumentNullException) { messages.NoKeyError(); } else { messages.UnexpectedError(); } } // countdown until program will close methods.Countdown(30); }