Exemplo n.º 1
0
        public static void DecryptMode(bool _retry = false)
        {
            if (_retry)
            {
                DisplayHeader("Invalid Input! Try again!");
            }
            else
            {
                DisplayHeader();
            }
            try
            {
                Key        cipher = new Key();
                Ciphertext ctext  = new Ciphertext();

                byte[] ciphertext = null;
                byte[] key        = null;
                byte[] iv         = null;

                string input;
                Console.Write("How would you like to input ciphertext? (FILE / TEXT): ");
                input = Console.ReadLine().ToUpper();

                if (input == "TEXT")
                {
                    Console.Write("Ciphertext: ");
                    ciphertext = Convert.FromBase64String(Console.ReadLine());
                }
                else if (input == "FILE")
                {
                    Console.WriteLine(@"Please enter the path to your ciphertext. (Ex. C:\EncrpytedText.AESTXT):");
                    string path = Console.ReadLine();
                    System.Xml.Serialization.XmlSerializer reader = new System.Xml.Serialization.XmlSerializer(typeof(Ciphertext));
                    System.IO.StreamReader file = new System.IO.StreamReader(path);
                    ctext      = (Ciphertext)reader.Deserialize(file);
                    ciphertext = Convert.FromBase64String(ctext.CIPHERTEXTSTRING);
                }
                else
                {
                    DecryptMode(true);
                }


                Console.Write("How would you like to input a key? (FILE / TEXT): ");
                input = Console.ReadLine().ToUpper();

                if (input == "TEXT")
                {
                    Console.Write("Key: ");
                    key = Convert.FromBase64String(Console.ReadLine());
                    Console.Write("IV: ");
                    iv = Convert.FromBase64String(Console.ReadLine());
                }
                else if (input == "FILE")
                {
                    Console.Write(@"Please enter the path to your key. (Ex. C:\MainKey.AES): ");
                    string path = Console.ReadLine();
                    System.Xml.Serialization.XmlSerializer reader = new System.Xml.Serialization.XmlSerializer(typeof(Key));
                    System.IO.StreamReader file = new System.IO.StreamReader(path);
                    cipher = (Key)reader.Deserialize(file);
                    key    = Convert.FromBase64String(cipher.KEY);
                    iv     = Convert.FromBase64String(cipher.IV);
                }
                else
                {
                    DecryptMode(true);
                }

                using (AesManaged myAes = new AesManaged())
                {
                    // TODO: Allow the text to be exported to a file.
                    // Decrypt the bytes to a string.
                    string roundtrip = DecryptStringFromBytes_Aes(ciphertext, key, iv);

                    DisplayHeader();
                    Console.Write("How would you like to output your text? (FILE / TEXT): ");
                    input = Console.ReadLine().ToUpper();

                    if (input == "TEXT")
                    {
                        Console.WriteLine("{0}", roundtrip);
                    }
                    else if (input == "FILE")
                    {
                        Console.WriteLine(@"Please enter the path to save your text. (Ex. C:\Document.txt):");
                        string path = Console.ReadLine();
                        System.IO.File.WriteAllText(path, roundtrip);
                    }
                    else
                    {
                        DecryptMode(true);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: {0}", e.Message);
            }
            Console.Write("Press any key to continue...");
            Console.ReadKey();
            OperationMode();
        }
Exemplo n.º 2
0
        public static void EncryptMode(bool _retry = false)
        {
            if (_retry)
            {
                DisplayHeader("Invalid Input!");
            }
            else
            {
                DisplayHeader();
            }

            try
            {
                string input;
                string original = null;
                // TODO: Allow text to be inputed from a file
                Key cipher = new Key();
                Console.Write("How would you like to input the text to Encrypt? (FILE / CONSOLE): ");
                input = Console.ReadLine().ToUpper();
                if (input == "FILE")
                {
                    Console.Write(@"Please enter the path to your text. (Ex. C:\TextDocument.txt): ");
                    original = System.IO.File.ReadAllText(Console.ReadLine());
                }
                else if (input == "CONSOLE")
                {
                    Console.Write("Text to Encrypt: ");
                    original = Console.ReadLine();
                }
                else
                {
                    EncryptMode(true);
                }

                byte[] key = null;
                byte[] iv  = null;

                Console.Write("How would you like to input a key? (FILE / TEXT): ");
                input = Console.ReadLine().ToUpper();

                if (input == "TEXT")
                {
                    Console.Write("Key: ");
                    key = Convert.FromBase64String(Console.ReadLine());
                    Console.Write("IV: ");
                    iv = Convert.FromBase64String(Console.ReadLine());
                }
                else if (input == "FILE")
                {
                    Console.Write(@"Please enter the path to your key. (Ex. C:\MainKey.AES): ");
                    string path = Console.ReadLine();
                    System.Xml.Serialization.XmlSerializer reader = new System.Xml.Serialization.XmlSerializer(typeof(Key));
                    System.IO.StreamReader file = new System.IO.StreamReader(path);
                    cipher = (Key)reader.Deserialize(file);
                    key    = Convert.FromBase64String(cipher.KEY);
                    iv     = Convert.FromBase64String(cipher.IV);
                }
                else
                {
                    EncryptMode(true);
                }

                // Create a new instance of the AesManaged
                // class.  This can generate a new key and
                // initialization vector (IV).
                using (AesManaged myAes = new AesManaged())
                {
                    byte[] encrypted;
                    string example;

                    // Encrypt the string to an array of bytes.
                    encrypted = EncryptStringToBytes_Aes(original, key, iv);
                    example   = Convert.ToBase64String(encrypted);

                    //Display the original data and the decrypted data.
                    DisplayHeader();
                    Console.Write("Where would you like to output to? (CONSOLE/FILE): ");
                    string line = Console.ReadLine().ToUpper();
                    if (line == "CONSOLE")
                    {
                        DisplayHeader();
                        Console.WriteLine("Cyphertext: {0}", example);
                    }
                    else if (line == "FILE")
                    {
                        DisplayHeader();
                        Ciphertext cyphertext = new Ciphertext();
                        cyphertext.CIPHERTEXTSTRING = example;
                        Console.Write(@"Please enter a path (Ex. C:\EncrpytedText.AESTXT): ");
                        string path = Console.ReadLine();
                        System.Xml.Serialization.XmlSerializer writer = new System.Xml.Serialization.XmlSerializer(typeof(Ciphertext));
                        System.IO.StreamWriter file = new System.IO.StreamWriter(path);
                        writer.Serialize(file, cyphertext);
                        file.Close();
                        Console.WriteLine("File successfully written to file at (" + path + ")");
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: {0}", e.Message);
            }
            Console.Write("Press any key to continue...");
            Console.ReadKey();
            OperationMode();
        }