static public void EncryptFile(string inputFile, string outputFile) { try { Console.WriteLine("\r\nChoose file to encrypt:\r"); inputFile = Console.ReadLine(); Console.WriteLine("\r\nSave encrypted file as:\r"); outputFile = Console.ReadLine(); Console.WriteLine("\r\nEnter a password and don\'t forget it!"); string prepass = HideMe.ReadPassword(); Console.WriteLine("\n\rPlease confirm your password"); string password = HideMe.ReadPassword(); if (password.Equals(prepass)) { UnicodeEncoding UE = new UnicodeEncoding(); byte[] key = SHA256.Create().ComputeHash(UE.GetBytes(Seeds.keySalt + password)); byte[] iv = SHA256.Create().ComputeHash(UE.GetBytes(Seeds.ivSalt + password)).Take(16).ToArray(); string cryptFile = outputFile; FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create); RijndaelManaged RMCrypto = new RijndaelManaged(); CryptoStream cs = new CryptoStream(fsCrypt, RMCrypto.CreateEncryptor(key, iv), CryptoStreamMode.Write); FileStream fsIn = new FileStream(inputFile, FileMode.Open); int data; while ((data = fsIn.ReadByte()) != -1) { cs.WriteByte((byte)data); } fsIn.Close(); cs.Close(); fsCrypt.Close(); Colors.SuccessText("\r\nSuccessfully encrypted " + "<" + inputFile + ">" + " as " + "[" + outputFile + "]" + "\n\rPress anykey.."); ClearTerminal.ClearToMain(); } else { Console.Clear(); Colors.FailText("PASSWORDS DO NOT MATCH!!! YOUR PASSWORD CAN NOT BE RECOVERED!\n\rIf you encrypt data and lose" + " the password you will lose the data forever.\n\rTo prevent any confusion we will start over. No " + "files have been changed. Press anykey to continue.."); ClearTerminal.ClearToMain(); } } catch { Console.Clear(); Colors.FailText("ERROR! Something went wrong\n\rPress anykey.."); ClearTerminal.ClearToMain(); } }
static public void DecryptFile(string inputFile, string outputFile) { Console.WriteLine("\r\nFilename to decrypt:\r"); inputFile = Console.ReadLine(); Console.WriteLine("\r\nSave decrypted file as:\r"); outputFile = Console.ReadLine(); Console.WriteLine("\r\nDid you remember your password? \n\r"); string password = HideMe.ReadPassword(); UnicodeEncoding UE = new UnicodeEncoding(); byte[] key = SHA256.Create().ComputeHash(UE.GetBytes(Seeds.keySalt + password)); byte[] iv = SHA256.Create().ComputeHash(UE.GetBytes(Seeds.ivSalt + password)).Take(16).ToArray(); try { FileStream fsCrypt = new FileStream(inputFile, FileMode.Open); RijndaelManaged RMCrypto = new RijndaelManaged(); CryptoStream cs = new CryptoStream(fsCrypt, RMCrypto.CreateDecryptor(key, iv), CryptoStreamMode.Read); FileStream fsOut = new FileStream(outputFile, FileMode.Create); int data; try { while ((data = cs.ReadByte()) != -1) { fsOut.WriteByte((byte)data); } fsOut.Close(); cs.Close(); fsCrypt.Close(); Colors.SuccessText("\r\nSuccessfully decrypted " + "<" + inputFile + ">" + " as " + "[" + outputFile + "]" + "\n\rPress anykey.."); ClearTerminal.ClearToMain(); } catch { Colors.FailText("Did you enter the correct password?\n\r Press anykey.."); ClearTerminal.ClearToMain(); } } catch { Colors.FailText("Does the file exist?\n\rPress anykey.."); ClearTerminal.ClearToMain(); } }
public static void Init(string x = "1", string y = "2", string z = "3", string inputFile = "", string outputFile = "") { string selec = Console.ReadLine(); if (selec.Equals(x)) { Encrypt.EncryptFile(inputFile, outputFile); } else if (selec.Equals(y)) { Decrypt.DecryptFile(inputFile, outputFile); } else if (selec.Equals(z)) { Environment.Exit(0); } else { Console.WriteLine("You can\'t read?\nPress anykey.."); } ClearTerminal.ClearToMain(); }