Пример #1
0
        //setup up database, make sure we can hit it or make the tables, 90% sure this will work
        private static bool scan_for_db()
        {
            MySQL_Interface MySQL = new MySQL_Interface(); //this is my layer over mysql connector
            List<string> databases = MySQL.mysql_select_database(my_username,my_password);
            bool found_keys = false;// looking for different tables
            bool found_data = false;
            foreach(string db in databases)
            {
                if (db == my_database)
                {
                    //found database
                    List<string> tables = MySQL.mysql_select_table(my_username,my_password,my_database);
                    foreach (string table in tables)
                    {
                        if (table == "keys")
                        {
                            found_keys = true;
                        }else{
                            if (table == "data")
                            {
                                found_data = true;
                            }
                        }
                    }
                }
            }
            if (found_data && found_keys) // if both tables are there just return true
            {
                return true;
            }

            //Tables Need made
            Console.Write("Database was found? Can I make a 'keys' and 'data' table in " + my_database + "?[y/n] "); //asking is always nice
            if (Console.ReadKey().KeyChar == 'y')
            {
                //make tables
                if (!found_keys)
                {
                    string command_text = "CREATE TABLE  `" + my_database + "`.`" + my_db_table + "` ( ";
                        command_text += @"	`index` INT( 10 ) NOT NULL AUTO_INCREMENT ,
                                             `modulus` VARCHAR( 256 ) NOT NULL ,
                                             `exponent` INT( 6 ) NOT NULL ,
                                             `active` TINYINT( 1 ) NOT NULL ,
                                             `date_added` INT( 10 ) NOT NULL ,
                                             `revoked` INT( 2 ) NOT NULL ,
                                            PRIMARY KEY (  `index` )
                                            ) ENGINE = INNODB DEFAULT CHARSET = latin1;";
                    MySQL.mysql_nonquery(my_username, my_password, my_database, command_text);

                    command_text = @"SELECT COUNT(`index`) AS items FROM `" + my_db_table + "`";

                    List<List<string>> return_stuff = MySQL.mysql_query(my_username,my_password,my_database,command_text);

                    if (return_stuff.Count > 0)
                    {
                        found_keys = true;
                    }
                }

                if (!found_data)
                {
                    string command_text = "CREATE TABLE  `" + my_database + "`.`" + my_db_data + "` ( ";
                        command_text += @"`index` INT NOT NULL AUTO_INCREMENT ,
                                            `data` TEXT NOT NULL ,
                                            `key_used` INT NOT NULL ,
                                            PRIMARY KEY (  `index` )
                                            ) ENGINE = MYISAM ;";
                    MySQL.mysql_nonquery(my_username, my_password, my_database, command_text);

                    command_text = @"SELECT COUNT(`index`) AS items FROM `" + my_db_data + "`";

                    List<List<string>> return_stuff = MySQL.mysql_query(my_username,my_password,my_database,command_text);

                    if (return_stuff.Count > 0)
                    {
                        found_data = true;
                    }
                }

                if(found_keys && found_data)
                {
                    //Foudn everything good to go
                    return true;
                }else{
                    return false;
                }
            }
            return false; //this shouldnt be hit, compiler complained
        }
Пример #2
0
        //show all the string unencrypted
        private static void option4_show_unencrypted()
        {
            MySQL_Interface MySQL = new MySQL_Interface();

            string command_text = @"SELECT * FROM `" + my_database + "`.`" + my_db_data + "`";

            List<List<string>> return_stuff = MySQL.mysql_query(my_username,my_password,my_database,command_text);

            if (return_stuff.Count > 0)
            {
                Console.Write(Environment.NewLine +  "||IND||   data          || key used " + Environment.NewLine);
                for (int i = 0; i < return_stuff.Count; i++)
                {
                    Console.Write("|| " + return_stuff[i][0] + " || ");
                    Console.Write(decrypt ((new System.IO.DirectoryInfo(System.Environment.CurrentDirectory)).ToString(), int.Parse(return_stuff[i][2]), return_stuff[i][1]));
                    Console.Write (" ||     "  + return_stuff[i][2] + Environment.NewLine);
                }
            }
        }
Пример #3
0
        //show the key table for funzies
        private static void option2_get_key_table()
        {
            MySQL_Interface MySQL = new MySQL_Interface();

            string command_text = @"SELECT * FROM `" + my_database + "`.`" + my_db_table + "`";

            List<List<string>> return_stuff = MySQL.mysql_query(my_username,my_password,my_database,command_text);

            if (return_stuff.Count > 0)
            {
                Console.Write(Environment.NewLine +  "||IND||   Modulus          || Exponent || Active || Revoked ||" + Environment.NewLine);
                for (int i = 0; i < return_stuff.Count; i++)
                {
                    Console.Write("|| " + return_stuff[i][0] + " || "  + return_stuff[i][1].Substring(0, 34) + " ||     "  + return_stuff[i][2] + " || "  + return_stuff[i][3] + " || " + return_stuff[i][4] + " || " + Environment.NewLine);
                }
            }
        }
Пример #4
0
        //show all the strings encrypted, for fun
        private static void option3_show_encrypted()
        {
            MySQL_Interface MySQL = new MySQL_Interface();

            string command_text = @"SELECT * FROM `" + my_database + "`.`" + my_db_data + "`";

            List<List<string>> return_stuff = MySQL.mysql_query(my_username,my_password,my_database,command_text);

            if (return_stuff.Count > 0)
            {
                Console.Write(Environment.NewLine +  "||IND||   data          || key used " + Environment.NewLine);
                for (int i = 0; i < return_stuff.Count; i++)
                {
                    Console.Write("|| " + return_stuff[i][0] + " || "  + return_stuff[i][1] + " ||     "  + return_stuff[i][2] + Environment.NewLine);
                }
            }
        }
Пример #5
0
        //this will add new keys, then deactive the others
        private static void option1_create_key()
        {
            MySQL_Interface MySQL = new MySQL_Interface();

            string Errors = "";
            //System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
            System.Console.Write("Creating Keys..." + Environment.NewLine);
            System.Security.Cryptography.RSACryptoServiceProvider provider = new System.Security.Cryptography.RSACryptoServiceProvider(); //make keys in c#
            System.Console.Write("Keys Created." + Environment.NewLine);

            System.Console.WriteLine("Connecting to database and deactivating past keys...");
            if (MySQL.mysql_clear_old_key(my_username,my_password,my_database))
            {
                Console.WriteLine("Old Keys Deactivated");
            }
            else
            {
                Errors += "Error disabling old keys" + "|" + "Run command 'database-maintance'" + "|";
                Console.WriteLine("Error disabling old keys");
                Console.WriteLine("Manually deactivte old keys or empty table, im not a english major spelling is for computers, and awake people");
            }

            System.Console.WriteLine("Inserting new key, and activating...");
            int time = Convert.ToInt32((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds); // get unix time stamp for easy use
            string mod = BitConverter.ToString(provider.ExportParameters(false).Modulus).Replace("-", string.Empty).ToLower(); //public key parts of ssl
            string expon = BitConverter.ToString(provider.ExportParameters(false).Exponent).Replace("-", string.Empty);

            //Public key parts get put in database to be used in web end, the rest of the key is saved to the hard drive so that just a local app can decrypt

            string mysql_command = "INSERT INTO `" + my_database + "`.`" + my_db_table +  "` (`index` ,`modulus` ,`exponent`,`active`,`date_added`) VALUES (NULL , '" + mod + "', '" + expon + "',  '1',  '" + time + "')";
            if (MySQL.mysql_nonquery(my_username, my_password, my_database, mysql_command))
            {
                //Save local key pairs
                string index_of = MySQL.mysql_select_cell(my_username, my_password, my_database, "SELECT * FROM `" + my_database + "`.`" + my_db_table +  "` WHERE `modulus`='" + mod + "' AND `date_added`='" + time + "'", "index");
                System.IO.DirectoryInfo Working_dir = new System.IO.DirectoryInfo(System.Environment.CurrentDirectory);
                string working_dir = Working_dir.ToString();
                local_store_key(working_dir, index_of, provider);
            }
            else
            {
                Console.Write("Error putting key in database" + Environment.NewLine);
            }
        }