0

Licensing Plugins – Part IV – The license generator

Writing a plugin that extends the functionality of CINEMA 4D is often only the first step towards a finished product. At one point or another during the work on your plugin you will have to spend some thought a proper license system.

This fourth article shows how to write a license generator, that will create keys we can use to unlock the plugin.

License generator

The simplest way to write a license generator is probably to write a little executable binary that takes the user’s information (C4D serial, or whatever) and spits out a license string. Just a simple command line tool. Or you could also write it as a separate little CINEMA 4D plugin. That way, we could use all the license source we have written in the previous chapters. It could also be as simple as a script file (JavaScript, Python, VBscript). We have a lot of opportunities to choose from.

Here is a very basic command line tool that does the job.

#include <iostream>
#include "md5.h"

int main(int argc, const char * argv[])
{
  if (argc != 2)
  {
    std::cout << "No C4D number provided";
      return 0;
  }
  
  char prefix[] = "MYPLUGIN";
  char* inputStr = strcat(prefix, argv[1]);
  DigestMD5 md5;
  char* key = md5.get_md5hash(inputStr, 11);
  if (!key)
  {
    std::cout << "Error";
    return 0;
  }
  
  std::cout << "Input String: " << inputStr << "n" << "License String: " << key << "n";
  
  return 0;
}

Call it via the Terminal (on OS X):
licgen 14400001241
or using the Shell on Windows:
licgen.exe 14400001241

However, that kind of Standalone license generators often has one problem: They don’t run on all machines. You wrote it on the Windows PC, and you don’t have it on your MacBook and your customers keep asking for their licenses? You wrote it as a Python script, but you can’t run Python on your mobile phone? All these problems, that always tend to occur when least expected, are the reason why I like and recommend the PHP-based online solution.

Online generator with PHP

Here is a short and simple PHP script that will generate licenses from C4D serials. It will also ask for a password, which is currently simply hardcoded to “1234” (can be changed in function pwcheck()). The license key is generated in function createkey(), and it is done exactly the same way as in MyLicense:CreateKey().

Call the file “licgen.php” (or change the filenames in the HTML forms appropriately).

<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<title>License Generator</title>
<style type='text/css'>
body {
  font-family: Arial, Helvetica, sans-serif;
  font-size: 12px;
}
</style>
</head>

<?php
  // check for user password
  function pwcheck($pw)
  {
    if ($pw == '1234')
      return true;
    else
      return false;
  }
	
  // Generate key from C4D serial
  function createkey($ser)
  {
    $lic_prefix = 'MYPLUGIN';

    $keyinput = $lic_prefix.substr($ser,-5);
    $keymd5 = md5($keyinput);
		
    return strtoupper($keymd5);
  }
	
  // Build the web form
  function buildform()
  {
    // Get passwort and serial
    $pw = $_POST['pw'];
    $ser = $_POST['ser'];
		
    $pass = pwcheck($pw);
		
    if ($pass && !empty($ser))
    {
      $key = createkey($ser);
			
      // Valid password & serial entered
      echo('<h1>License key</h1>'.'n');
      echo('<p>Key('.$ser.'):<br/><b>'.$key.'</b></p>');

      echo('<form action='licgen.php' method='post'>'.'n');
      echo('<input type='hidden' name='pw' value=''.$pw.''>'.'n');
      echo('<input type='hidden' name='ser' value=''>'.'n');
      echo('<input type='submit' value='New' name='new' alt='New'>'.'n');
      echo('</form>'.'n');
    }
    else
    {
      // Invalid or no password -> show password form
      echo('<h1>Enter password and serial</h1>'.'n');
      echo('<form action='licgen.php' method='post'>'.'n');
      echo('<table>'.'n');
      echo('<tr>'.'n');
      echo('<td>Password:</td><td><input type='password' name='pw' maxlength='60'></td>'.'n');
      echo('</tr>'.'n');
      echo('<tr>'.'n');
      echo('<td>Serial:</td><td><input type='text' name='ser' maxlength='11' value=''.$ser.''></td>'.'n');
      echo('</tr>'.'n');
      echo('<tr>'.'n');
      echo('<td></td><td><input type='submit' value='Submit' name='submit' alt='Submit'>');
      echo('</td>'.'n');
      echo('</tr>'.'n');
      echo('</table>'.'n');
      echo('</form>'.'n');
    }
  }
?>

<body>
<?php
  buildform();
?>
</body>
</html>

The password

The password is simply to prevent unauthorized users from generating licenses. It is not included in the license key, it just needs to be provided for a license to be generated.

Of course, this is not a 100% secure solution. It is recommended that you take further measures to secure this page on your server (HTTPS, additional password using .htaccess, et cetera). This simple password example is just meant for demonstration.

createkey()

This function is basically an exact copy of MyLicense::CreateKey(), just translated to PHP.
The fact that MD5 is available in PHP out of the box, is another reason why I chose MD5 as the key algorithm in this article.

buildform()

This function simply spits out the HTML code that displays the page. If a password and serial have been provided, it will present the results, otherwise it will just generate input fields for password and serial.

So that’s all we need for our online license generator. We can now upload the page on a web server, and access it from practically anywhere in the world. It will take our password and a C4D serial and output a license key. That key, we can now put into a mail and send it to the customer.

You should run CINEMA 4D now, and try out the license code.

Conclusion

Now we have developed a pretty comfortable solution. Our plugin needs a license to work in the CINEMA 4D full version, but runs free in the CINEMA 4D DEMO, NETrender and the Command line version. We also have a license generator that allows us to create license files for customers. If we wanted, we could even make all existing licenses invalid with the next update of the plugin, by simply changing MYLICENSE_PREFIX in license.cpp (and prefix[] in the license generator’s main.cpp; or $lic_prefix in licgen.php).

This solution might be sufficient for most people. However, the next part of this article will show some ways to extend the functionality and flexibility of the license system.

Related articles

Frank Willeke

worked with computers since more than 20 years | got hooked on computer graphics back on the AMIGA | started programming at the age of 13 | relesed some successful plugins for cinema 4d | started working for maxon computer gmbh in 2009 | now contributing to cinema 4d as a senior developer making electronic music since 1993 | playing the electric guitar since 1995 age of 14 | first live gigs in 1997 | playing the bass guitar 2005 | playing keyboards and synths since 2012 experimenting with photography since 2003

Leave a Reply

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