+ Reply to Thread
Results 1 to 3 of 3

Thread: Create a License key feature for your application

  1. #1

    Create a License key feature for your application

    Can you give a tutorial of how to integrate a licence key feature into my application ?
    Last edited by r.vector; 11-01-2012 at 03:05 PM.

  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    24
    1) Create a License key feature for your application
    Ever wanted to add a license key feature to your application ?
    It is simple but will take a lot of time from you
    Because you need to come up with a good algorithm that will be hard to be broken.

    What do you need ?
    1) Key Generator.
    2) Key and DateValidation function in client application
    3) Register the information of client in registry or somewhere on the hard drive so you make sure he already
    entered a registration information.

    Before getting to Key Gen function now , the product key can be on any format like for example :
    Code:
    X X X X  -  X X X X  -  X X X X  -  X X X X
    --------    --------    --------    --------
    Prefix      M M D D     Y Y Y Y      Postfix
    Prefix can be any word to make sure that this is the key for that application.
    Prefix can be anything like : Application name or any kind of Prefix
    M M Refer to Month
    D D Refer to Day
    Y Y Y Y Refer to Year
    Postfix can be any random string.

    Now it wont be logical to send the product key like that.
    Any kid can understand the algorthim here.
    So what you can do is replacing each char with another char.
    For example :
    Original Key : ABCD - EFGH - IJKL - MNOP
    Product Key : PLHD - OKGC - JJFB - MIEA
    I guess you got it.

    1) Key Generator Function :
    -----------------------------
    I`ll be giving the Create_Key date like : ABCDEFGHIJKLMNOP and it will replace everything and add "-" after each 4 chars
    Code:
            private string Create_Key(string data)
            {
                byte[] bytes = Encoding.ASCII.GetBytes(data);
                string KEY = string.Empty;
    
                KEY += data[15];
                KEY += data[14];
                KEY += data[8];
                KEY += data[9];
                KEY += "-";
                KEY += data[6];
                KEY += data[11];
                KEY += data[4];
                KEY += data[12];
                KEY += "-";
                KEY += data[2];
                KEY += data[3];
                KEY += data[13];
                KEY += data[5];
                KEY += "-";
                KEY += data[7];
                KEY += data[10];
                KEY += data[1];
                KEY += data[0];
    
                return KEY;
            }
    In client application the client will provide a product key , you need to follow these steps to ensure that the key is valis
    1) Check the prefix of the key
    2) If prefix is correct then it is a valid key then lets Check the expiration Date of the key
    3) If key is not yet expired then open appliation.

    2 - a) Key Validation Function : [Read Prefix]
    -----------------------------------------------
    The key Validation function will be the same Key Generator function which is >>>Create_Key() ,
    Because we are using the same algorthim in key generation process.

    Step one : reverse the the product key because we need the original Key to read prefix.
    Code:
            private string Create_Key(string Product_Key)
            {
                byte[] bytes = Encoding.ASCII.GetBytes(Product_Key);
                string KEY = string.Empty;
    
                KEY += Product_Key[15];
                KEY += Product_Key[14];
                KEY += Product_Key[8];
                KEY += Product_Key[9];
                KEY += "-";
                KEY += Product_Key[6];
                KEY += Product_Key[11];
                KEY += Product_Key[4];
                KEY += Product_Key[12];
                KEY += "-";
                KEY += Product_Key[2];
                KEY += Product_Key[3];
                KEY += Product_Key[13];
                KEY += Product_Key[5];
                KEY += "-";
                KEY += Product_Key[7];
                KEY += Product_Key[10];
                KEY += Product_Key[1];
                KEY += Product_Key[0];
    
                return KEY;
            }
    Step two :
    Create a Bool function to retun the legit status of the key :
    Code:
            private bool validate_key(string key)
            {
                string[] seperate_filelds = key.Split('-');
                int counter=0;
                bool is_legit = false;
    
                foreach (string each_field in seperate_filelds)
                {
                    counter++;
    				// reading the first field which is prefix field
                    if (counter == 1)
                    {
                        if (each_field == "Your_Prefix")
                        {
                            is_legit = true;
                        }
                    }
                    else
                    { break; }
                }
    
                return is_legit;
            }

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    24
    After you made sure that the key is valid , move on to the next verification which is exp.date verification
    2 - b) Date Validation Function :
    ----------------------------------
    Code:
            private string Validate_Date(string key)
            {
                try
                {
                    string[] seperate_filelds = key.Split('-');
                    int counter = 0;
                    string date = string.Empty;
                    string status = string.Empty;
    
                    // get date in format of MM/DD/YYYY
                    foreach (string each_field in seperate_filelds)
                    {
                        counter++;
                        if (counter == 2)
                        {
                            date += each_field[0];
                            date += each_field[1];
                            date += "/";
                            date += each_field[2];
                            date += each_field[3];
                            date += "/";
                            date += each_field[4];
                            date += each_field[5];
                            date += each_field[6];
                            date += each_field[7];
    
                            break;
                        }
                    }
    
                    // date format : MM/DD/YYYY
                    string[] get_expireyear = date.Split('/');
                    int counter1 = 0;
                    foreach (string current_expire_year in get_expireyear)
                    {
                        counter1++;
                        if (counter1 == 3)
                        {
                            if (Convert.ToInt16(current_expire_year) >= Convert.ToInt16(DateTime.Now.ToString("yyyy")))
                            {
                                if (Convert.ToInt16(current_expire_year) == Convert.ToInt16(DateTime.Now.ToString("yyyy")))
                                {
                                    string[] get_expiremonth = date.Split('/');
                                    int counter2 = 0;
                                    foreach (string current_expire_month in get_expiremonth)
                                    {
                                        counter2++;
                                        if (counter2 == 1)
                                        {
                                            if (Convert.ToInt16(current_expire_month) >= Convert.ToInt16(DateTime.Now.ToString("MM")))
                                            {
                                                if (Convert.ToInt16(current_expire_month) == Convert.ToInt16(DateTime.Now.ToString("MM")))
                                                {
                                                    string[] get_expireday = date.Split('/');
                                                    int counter3 = 0;
                                                    foreach (string current_expire_day in get_expireday)
                                                    {
                                                        counter3++;
                                                        if (counter3 == 2)
                                                        {
                                                            if (Convert.ToInt16(current_expire_day) >= Convert.ToInt16(DateTime.Now.ToString("dd")))
                                                            {
                                                                if (Convert.ToInt16(current_expire_day) == Convert.ToInt16(DateTime.Now.ToString("dd")))
                                                                {
                                                                    status = "Expired";
                                                                    break;
                                                                }
                                                                else
                                                                {
                                                                    status = "Valid";
                                                                    break;
                                                                }
                                                            }
                                                            else
                                                            {
                                                                status = "Expired";
                                                                break;
                                                            }
                                                        }
                                                    }
                                                }
                                                else
                                                {
                                                    status = "Valid";
                                                    break;
                                                }
                                            }
                                            else
                                            {
                                                status = "Expired";
                                                break;
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    status = "Valid";
                                    break;
                                }
                            }
                            else
                            {
                                status = "Expired";
                                break;
                            }
                        }
                    }
                    return status;
                }
                catch { return "Invalid"; }
            }
    Finally : [Add that code]
    Code:
                        string Product_Key = textBox1.Text;
                        Product_Key = Product_Key.Replace("-", "");
    
                        Program p = new Program();
                        if (p.validate_key(p.Get_Original_Key(Product_Key)))
                        {
                            string key_status = p.Validate_Date(p.Get_Original_Key(Product_Key));
    
                            if (key_status == "Valid")
                            {
                                // run application or do anything here.........
                            }
                            else if (key_status == "Invalid")
                            {
                                MessageBox.Show("The Product key is invalid , please try a new one!", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                            else if (key_status=="Expired")
                            {
                                MessageBox.Show("The Product key has Expired , please renew now!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                        }
                        else
                        {
                            MessageBox.Show("The Product key is invalid , please try a new one!", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts