Ejemplo n.º 1
0
        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();
            }
        }
Ejemplo n.º 2
0
        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();
            }
        }