Friday, 15 June 2012

Tagged under: , , ,

Connecting C# to MySQL database / C# connection String for MySQL

In many cases it becomes imperative to connect C# to MySql, the prominent reason being the inherent simplicity of MySql. We have generally used the usual connection string to connect PHP to MySql. Connecting C# to MySql is similar.
We first need to use the connection string to establish the connection with MySql. Then, we use the SQL queries to carry out the Creation, Insertion, Update & Delete operations.


Before we do anything, first you need to download and install mysql-connector. You can get this connector from MySQL :: Download Connector/Net.

Once you install this connector, you need to add it as a reference in your C# project. You can do this by navigating to Project -> Add Reference. Then go to the .Net tab, and search for MySql.Data, and add it as a reference.

Now you need to include MySql.Data.MySqlClient at the start of the code. Add this at the start:
using MySql.Data.MySqlClient;



Now, you can use this connection string for establishing the connection:
string MyConString = "SERVER=localhost;" +    "DATABASE=dbname;" +    "UID=root;" +    "PASSWORD=\"\";";
MySqlConnection con = new MySqlConnection(MyConString);

con.Open();


Now, you can use 'con' when you need to fire any query.

Now, lets get into details of using sql queries.

1. CREATE TABLE:

   string query = "CREATE TABLE table_name";
   MySqlCommand cmd = new MySqlCommand(query, con);
   try
   {
     cmd.ExecuteNonQuery();
   }
   catch(Exception e)
   {
     Console.WriteLine(e);
   }
                 
2. INSERT:

   string query = "INSERT INTO table_name (attributes) VALUES(values)";
   MySqlCommand cmd = new MySqlCommand(query, con);
   try
   {
      cmd.ExecuteNonQuery();
   }
   catch(Exception e)
   {
      Console.WriteLine(e)
   }
                 
3. UPDATE:

   string query = " UPDATE table_name
   SET column1=value1, column2=value2, ...
   WHERE some_column=some_value";
   MySqlCommand cmd = new MySqlCommand(query, con);
   try
   {
     cmd.ExecuteNonQuery();
   }
   catch(Exception e)
   {
     Console.WriteLine(e)
   }
                 
4. DELETE:

   string query = "DELETE FROM table_name WHERE some_column=some_value";
   MySqlCommand cmd = new MySqlCommand(query, con);
   try
   {
     cmd.ExecuteNonQuery();
   }
   catch(Exception e)
   {
     Console.WriteLine(e)
   }

5. SELECT:

   MySqlCommand query = new MySqlCommand("Select * FROM `table_name` where
   CONDITION, con);
   MySqlDataReader reader = con.ExecuteReader();
   while (reader.Read())
   {
     Console.WriteLine(reader.GetString("column_name"));
   }

Finally, you need to close the connection, after you finish executing the queries. You can do this by writing:
con.Close();

                 


Friday, 1 June 2012

Tagged under: , , , ,

Generating a Unique Hardware Fingerprint for Software Licensing

Generating a hardware fingerprint is the most commonly sought technique to ensure security in case of softwares. There can be various methods of generating the hardware fingerprint, using different languages. However, the important thing to decide is, which devices to consider while generating the fingerprint.

For licensing purposes, the best and secure way is to generate a unique key for the client's machine and provide a corresponding license key for that key. The key can be generated using the unique id of the client's computer motherboard, BIOS and processor. When you get these IDs, you can generate any key of your preferable format.

In this post, I will get into the details of generating the unique fingerprint for the user's system.

Step I: Adding References:

First of all, you need to add the following references to your project.
1. System.Drawing
2. System.Management
3. System.Windows.Forms
For doing this, navigate to Project in the Menu Bar--> Add References-->.Net

Step II: Starting to Code

Add the following code at the start.
using System;
using System.Threading;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Resources;
using System.Globalization;
using System.Reflection;
using System.Text;
using System.Data;
using System.Drawing;


namespace Aj_fingerprint
{
    public class Aj_fp
    {
        public static void Main(string[] args)
        {
            Aj_Fingerprint f = new Aj_Fingerprint();
            string fp = f.Value();
            Console.WriteLine(fp);
            Console.ReadLine();
        }
    }


    public class Aj_Fingerprint
    {
        public string Value()
        {
            return pack(cpuId()+ biosId()+ diskId()+ baseId()+ videoId()+ macId());
        }
        
        private string biosId()
        {
            return identifier("Win32_BIOS", "Manufacturer")
            + identifier("Win32_BIOS", "SMBIOSBIOSVersion")
            + identifier("Win32_BIOS", "IdentificationCode")
            + identifier("Win32_BIOS", "SerialNumber")
            + identifier("Win32_BIOS", "ReleaseDate")
            + identifier("Win32_BIOS", "Version");
        }

        private string cpuId()
        {
            string retVal = identifier("Win32_Processor", "UniqueId");
            if (retVal == "")
            {
                retVal = identifier("Win32_Processor", "ProcessorId");

                if (retVal == "")
                {
                    retVal = identifier("Win32_Processor", "Name");


                    if (retVal == "")
                    {
                        retVal = identifier("Win32_Processor", "Manufacturer");
                    }

                    retVal += identifier("Win32_Processor", "MaxClockSpeed");
                }
            }

            return retVal;

        }

        private string diskId()
        {
            return identifier("Win32_DiskDrive", "Model")
            + identifier("Win32_DiskDrive", "Manufacturer")
            + identifier("Win32_DiskDrive", "Signature")
            + identifier("Win32_DiskDrive", "TotalHeads");
        }

        private string pack(string text)
        {
            string retVal;
            int x = 0;
            int y = 0;
            foreach (char n in text)
            {
                y++;
                x += (n * y);
            }

            retVal = x.ToString() + "00000000";

            return retVal.Substring(0, 8);
        }

        private string videoId()
        {
            return identifier("Win32_VideoController", "DriverVersion")
            + identifier("Win32_VideoController", "Name");
        }

        private string baseId()
        {
            return identifier("Win32_BaseBoard", "Model")
            + identifier("Win32_BaseBoard", "Manufacturer")
            + identifier("Win32_BaseBoard", "Name")
            + identifier("Win32_BaseBoard", "SerialNumber");
        }

        private string macId()
        {
            return identifier("Win32_NetworkAdapterConfiguration", "MACAddress", "IPEnabled");
        }

        private string identifier(string wmiClass, string wmiProperty)
        {
            string fprint = "";
            System.Management.ManagementClass aj_mc = new System.Management.ManagementClass(wmiClass);
            System.Management.ManagementObjectCollection aj_moc = aj_mc.GetInstances();
            foreach (System.Management.ManagementObject aj_mo in aj_moc)
            {
                if (fprint == "")
                {
                    try
                    {
                        fprint = aj_mo[wmiProperty].ToString();
                        break;
                    }
                    catch
                    { }
                }
            }
            return fprint;
        }

        private string identifier(string wmiClass, string wmiProperty, string wmiMustBeTrue)
        {
            string fprint = "";
            System.Management.ManagementClass aj_mc = new System.Management.ManagementClass(wmiClass);
            System.Management.ManagementObjectCollection aj_moc = aj_mc.GetInstances();
            foreach (System.Management.ManagementObject aj_mo in aj_moc)
            {
                if (aj_mo[wmiMustBeTrue].ToString() == "True")
                {
                    if (fprint == "")
                    {
                        try
                        {
                            fprint = aj_mo[wmiProperty].ToString();
                            break;
                        }
                        catch
                        { }
                    }

                }
            }
            return fprint;
        }


    }
}



Now, you can use this unique hardware fingerprint in further hashing, to authenticate the user system!!



You can for further assistance.