Simple C# Licensing (Rhino Licensing)

Share On

What is Software Licensing?

Software licenses typically provide users with the right to one or more copies of the software without violating copyrights. The license also defines the responsibilities of the parties entering into the license agreement and may impose restrictions on how the software can be used. Software licensing terms and conditions usually include the usages of the software, warranties and disclaimers and protections if the software or its use infringes on the intellectual property rights of others.

About Rhino Licensing –

Rhino Licensing uses Public-key cryptography, also known as asymmetric cryptography, it’s a class of cryptographic algorithms which requires two separate keys, one of which is secret (or private) and one of which is public. Although different, the two parts of this key pair are mathematically linked. The public key is used to encrypt plaintext or to verify a digital signature; whereas the private key is used to decrypt cipher text or to create a digital signature. The term “asymmetric” stems from the use of different keys to perform these opposite functions, each the inverse of the other – as contrasted with conventional (“symmetric”) cryptography which relies on the same key to perform both. You encrypt a value using the one key, and it can only be decrypted using the other key.

In the case of license key generation, we store our private key on the server, and distribute the public key with our application. When the user receives a license key, the application is able to verify that it came from us by using the public key. If someone tampers with the license key it will be invalid and the software will no longer execute.


Sample License Generator and Implementation

  • For the purpose of this demo and to save time i am not going to use any fancy re-factoring, patterns, validation and styling. I will leave that up to you. I also recommend you have a good understanding of the core C# language before tackling this project.
  1. Pre requisites – Download the Rhino Licensing source from https://github.com/ayende/rhino-licensing, you will need the core files to reference in your application. The full solution have unit test and samples included.
  2. Start by creating a new Windows form application, I am using Visual Studio 2013 for this demo.

Start by creating a new Windows form application-NewProject

  • Add the following items to your new form.
    1. Label and Text box for Full Name.
    2. Date time picker for expiry date.
    3. Combo box for license type
    4. Button to generate public and private keys.
    5. Button to create new license.
    6. Add a another text box for XML output and set the multiline attribute to true.
  • Feel free to name your forms and labels to your desire. Your form should be ready, see screen shot below

License Form name and labels, simple c licensing


Time to do some coding 

  1. We will start by adding another {.cs} file to our project. Name it Extension Methods
  • Add the following lines of code to the new extension methods class. The purpose of this methods is to have a generic way of creating select lists for a enum type.

ExtentionMethods - add lines of code to the new extension methods class

  • Add the following line of code to the form constructor to initialize the license combo box items.

constinitializer - add line of code to the form constructor

 

  •  Create a private method for generating the public and private keys. store this in a location of your choice and remember the private key must never get lost or exposed to 3rd parties. The private key is used to de-crypt the license file. You can store this on a database or in a private file storage system in your development environment. I recommend you do not distribute this private key with your applications.

Generatekey Create a private method for generating the public and private keys

  •  Create two Private Methods that will generate a new license XML file based on the public and private keys you have created before. The First method is a method that will extract the hardware Ids from your machine. this will help to tie the license to only one computer. For more key value pair codes on hardware id`s, visit http://www.codeproject.com/Articles/17973/How-To-Get-Hardware-Information-CPU-ID-MainBoard-I. This is an optional feature for the license file and i am using the overloaded method for generating the license file. You could omit the options if you choose not to tie down your license to one computer.
  • The second Method will generate the license and export it as a signed XML document. The reason we sign the document is to ensure that no tampering with the license will be allowed.

GetHardwareids second Method will generate the license

createlic - Simple C Licensing

  •  Finally, Hook up your private methods to your button click event handlers, Remember to first generate you public and private key and thereafter generate your license key. for the purpose of this demo you will see the XML generated on the XML text box, if you look in your folder location you used to save these files, you will find three XML files. Private, Public and License.

btn calls private methods to your button click event handlers -simple c licensing

 Validating the license in you applications

  • The final step is to add the license validation method in to your software source code. It could be in a start up method or every constructor you initialize. its up to you when and how you would like to validate the license. So I mention above that you need to get the Rhino licencing source code from https://github.com/ayende/rhino-licensing. If you study the core of their solution you will find the following source files. LicenseGenerator.cs, LicenseValidator.cs these two files together with their dependencies could be build in to a separate dll or as two separated dll`s or incorporated directly in your solution.
  • Insure that you supply a valid path to point to your public and license key files.
  • Lastly to validate the additional options you added via the Key value pair dictionary when you created the license, find the ValidateXmlDocumentLicense() method in the AbstractLicenseValidator.cs file. Here you can add additional logic and validation against your license file properties such as the BIOSId, CPUId, and machine name. You could potentially have encrypted values somewhere in your machine registry to validate against. The sky’s the limit.

licvalidation ValidateXmlDocumentLicense() method in the AbstractLicenseValidator.cs file

Last words

This type of licensing might not suite everyone’s needs and applications  but its definitely a good stepping stone to start with. Its a very simple and cost effective licensing model. Its strong enough for the average Joe not to Hack.


Share On

4 thoughts on “Simple C# Licensing (Rhino Licensing)”

  1. Looks like what I have been looking for. Would you please provide the code snippets in the tutorial above as text rather than images. Thanks.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
× How can I help you?