×
Namespaces

Variants
Actions

How to generate license key for Software with libcrypto library

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Code ExampleArticle
Created: mahbub_s60 (20 Sep 2010)
Last edited: hamishwillee (30 May 2013)

Contents

Introduction

This article explains how a developer can introduce license key in Symbian C++ application. This snippet shows how the OpenSSL crypto library can be used to create a license key that is needed to insert to the software for valid use. The License key is generated from the IMEI of the device and with the combination of a secret key (that is known only by the developer). Here one limitation is observed is that the secret key can't be longer than 8 characters long.


How to generate license key

IMEI is 15 digit unique number that can be read by application. The IMEI and the secret key is sent to the library provided by Open C (libcrypto.lib). From that library we get the descriptor that can be used as license key. The descriptor can be converted to hexadecimal code for readability.

if(iImeibuf.Length() > 0 && iImeibuf.Length() < 20)
{
if (iEncrypt8)
{
delete iEncrypt8;
iEncrypt8 = NULL;
}
 
iMyMsgToEncrypt8.Copy(iImeibuf);
iEncrypt8 = Encrypt(iMySecretKey8,iMyMsgToEncrypt8,iMyMsgToEncrypt8.Length());
_LIT(KFormat, "%x");
TBuf<50> LicenseKey(0);
TBuf<2> twoKey(0);
TInt length= iEncrypt8->Des().Length();
const TUint8 *Ptr =iEncrypt8->Des().Ptr();
 
 
for(TInt i = 0; i < length; i ++)
{
twoKey.Format(KFormat, *Ptr);
LicenseKey.Append(twoKey);
Ptr++;
}
 
CAknMessageQueryDialog* dlg = new (ELeave)CAknMessageQueryDialog();
dlg->PrepareLC(R_ABOUT_DESTEST_QUERY_DIALOG);
HBufC* title = iEikonEnv->AllocReadResourceLC(R_ABOUT_DIALOG_TITLE);
dlg->QueryHeading()->SetTextL(*title);
CleanupStack::PopAndDestroy(); //title
dlg->SetMessageTextL(LicenseKey);
dlg->RunLD();
}
 
 
/*
* Param aKey, maximum 8 char long very secret key
* Param aMsg, Imei of the device that can be read and pass to this function
* return: Pointer to license key data that can be converted to readable data
**/

 
 
HBufC8 * CDesTestAppUi::Encrypt( const TDesC8 & aKey, const TDesC8 & aMsg, TInt aSize)
{
 
HBufC8 *Res8 = NULL;
TInt n=0;
DES_cblock Key2;
DES_key_schedule schedule;
 
 
TText8 * Resin = new TText8 ( aSize );
 
 
 
Res8 = HBufC8::NewL(aSize);
 
/* Prepare the key for use with DES_cfb64_encrypt */
memcpy( Key2, aKey.Ptr(), aKey.Length());
DES_set_odd_parity( &Key2 );
DES_set_key_checked( &Key2, &schedule );
 
/* Encryption occurs here */
DES_cfb64_encrypt( ( unsigned char * ) aMsg.Ptr(), ( unsigned char * ) Resin, aSize, &schedule, &Key2, &n, DES_ENCRYPT );
TPtr8 hbufptr8 = Res8->Des();
 
Res8->Des().Copy((TUint8 * )Resin, aSize);
return (Res8);
}


Comparison of license key

Once the license key has been generated that can be distributed to user separately. When the user inserts the license key we need to compare the key with the one that was generated in the reverse way. In real application this should be done with a settings view. Following code shows how this can be done.

TBuf8<50> LicenseKey(0);
TBuf8<2> twoKey(0);
TInt length= iLicenseKeyGiven.Length();
 
const TUint8 *Ptr =iLicenseKeyGiven.Ptr();
for(TInt i = 0; i < length; i ++)
{
twoKey.SetLength(0);
twoKey.FillZ();
twoKey.Append(*Ptr);
Ptr++;
twoKey.Append(*Ptr);
Ptr++;
i++;
TLex8 lex8;
TUint8 val = 0;
lex8.Assign(twoKey);
TInt ret= lex8.Val(val, EHex);
TText8 tr = TText8 (val);
LicenseKey.Append(val);
}
 
if (iDecrypt8)
{
delete iDecrypt8;
iDecrypt8 = NULL;
}
iDecrypt8 = Decrypt(iMySecretKey8,LicenseKey, iMyMsgToEncrypt8.Length() );
if(iDecrypt8->Des().Compare(iMyMsgToEncrypt8)== 0 )
{
CAknInformationNote* note = new ( ELeave ) CAknInformationNote;
note->ExecuteLD( _L("Check OK") );
}
else
{
CAknInformationNote* note = new ( ELeave ) CAknInformationNote;
note->ExecuteLD( _L("Check Failed") );
}
 
 
 
/*
* Param aKey, maximum 8 char long very secret key
* Param aMsg, that need to decrypt
* return: Pointer to decrypted message
**/

 
HBufC8 * CDesTestAppUi::Decrypt( const TDesC8 & aKey, const TDesC8 & aMsg, TInt aSize)
{
HBufC8 *Res8 = NULL;
TInt n=0;
 
DES_cblock Key2;
DES_key_schedule schedule;
Res8 = HBufC8::NewL(aSize);
TText8 * Resin = new TText8 ( aSize );
 
/* Prepare the key for use with DES_cfb64_encrypt */
memcpy( Key2, aKey.Ptr(),aKey.Length());
DES_set_odd_parity( &Key2 );
DES_set_key_checked( &Key2, &schedule );
 
/* Decryption occurs here */
DES_cfb64_encrypt( ( unsigned char * ) aMsg.Ptr(), ( unsigned char * ) Resin, aSize, &schedule, &Key2, &n, DES_DECRYPT );
TPtr8 hbufptr8 = Res8->Des();
Res8->Des().Copy((TUint8 * )Resin, aSize);
return (Res8);
}


Using the Example

This example was tested with N97. First we need to get the license key and later the license key can be inserted to test if user has inserted right license key. If the inserted key is right then it shows a message that it license check was OK
Download the example: File:Licensecheck.zip

This page was last modified on 30 May 2013, at 07:34.
106 page views in the last 30 days.