Skip to content
master
Switch branches/tags
Code

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
src
 
 
 
 
 
 
 
 
 
 

Cryptolens Licensing for Java

This repository contains examples of how to verify license keys in Java applications.

This library is continuously being improved. Please contact us at support@cryptolens.io should you have any questions.

Javadoc can be found here: https://help.cryptolens.io/api/java/

Adding the library

There are two pre-compiled jar files: cryptolens.jar and cryptolens-android.jar. If your application is cross platform or if you would like to have as few dependencies as possible (e.g., without slf4j), we recommend to use cryptolens-android.jar instead.

If you choose to use cryptolens-android.jar, GetMachineCode and IsOnRightMachine need to be called with the version parameter set to 2. For example, Helpers.GetMachineCode(2) or Helpers.IsOnRightMachine(license, 2). If your application will run on an Android device, we recommend to use a different way to obtain the machine code, which is described here.

Example

Key verification

The following example is similar to what is covered in the key verification tutorial.

Assuming you have referenced the cryptolens.jar file, the code below should generate successful result. A working project with the code below can be found in the example-app folder.

import io.cryptolens.methods.*;
import io.cryptolens.models.*;

public class Main {

    public static void main(String[] args) {
        String RSAPubKey = "<RSAKeyValue><Modulus>sGbvxwdlDbqFXOMlVUnAF5ew0t0WpPW7rFpI5jHQOFkht/326dvh7t74RYeMpjy357NljouhpTLA3a6idnn4j6c3jmPWBkjZndGsPL4Bqm+fwE48nKpGPjkj4q/yzT4tHXBTyvaBjA8bVoCTnu+LiC4XEaLZRThGzIn5KQXKCigg6tQRy0GXE13XYFVz/x1mjFbT9/7dS8p85n8BuwlY5JvuBIQkKhuCNFfrUxBWyu87CFnXWjIupCD2VO/GbxaCvzrRjLZjAngLCMtZbYBALksqGPgTUN7ZM24XbPWyLtKPaXF2i4XRR9u6eTj5BfnLbKAU5PIVfjIS+vNYYogteQ==</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
        String auth = "WyIyNjA5IiwiaWE5b0VFT3Q2eDlNR2FvbHBHK2VOYUZ4bzNjT3h5UkNrMCtiYnhPRSJd";

        LicenseKey license = Key.Activate(auth, RSAPubKey, new ActivateModel(3349, "ICVLD-VVSZR-ZTICT-YKGXL", Helpers.GetMachineCode()));

        if (license == null || !Helpers.IsOnRightMachine(license)) {
            System.out.println("The license does not work.");
        } else {
            System.out.println("The license is valid!");
            System.out.println("It will expire: " + license.Expires);
        }
    }
}

In order to adjust this code to your products, several parameters needs to be changed, which we outline below:

  • RSAPubKey - the RSA Public key, which can be found on this page.
  • auth - the access token (can be found here, in API Keys section).
  • product_id - the id of the product can be found on the product page (in the example above, it's 3646).
  • key - the license key to be verified (above it's MPDWY-PQAOW-FKSCH-SGAAU).
  • machine_code - the unique id of the device, which may require root access. Note, this value is not the same as the one generated by the .NET client.

Note: The code above assumes that node-locking is enabled. By default, license keys created with Maximum Number of Machines set to zero, which deactivates node-locking. As a result, machines will not be registered and the call to Helpers.IsOnRightMachine(license) will return False. You can read more about this behaviour here. For testing purposes, please feel free to remove Helpers.IsOnRightMachine(license) from the if statement.

Offline activation (saving/loading licenses)

Assuming the license key verification was successful, we can save the result in a file so that we can use it instead of contacting Cryptolens.

String licenseString = license.SaveAsString();

When loading it back, we can use the code below:

LicenseKey newLicense = LicenseKey.LoadFromString(RSAPubKey, licenseString);

If you want to make sure that the license file is not too old, you can specify the maximum number of days as shown below (after 30 days, this method will return null).

LicenseKey newLicense = LicenseKey.LoadFromString(RSAPubKey, licenseString, 30);

Note: LicenseKey.LoadFromString does not check the ProductId. In case you have multiple products, we recommend that you check that the ProductId corresponds to the product where the user tries to use the license file.

Floating licenses

Floating licenses can be enabled by passing a floatingTimeInterval to the ActivateModel. Optionally, you can also allow customers to exceed the bound by specifying the maxOverdraft.

The code below has a floatingTimeInterval of 300 seconds and maxOverdraft set to 1. To support floating licenses with overdraft, the call to Helpers.IsOnRightMachine(license, true, true) needs two boolean flags to be set to true.

import io.cryptolens.methods.*;
import io.cryptolens.models.*;

public static void main(String args[]) {
    String RSAPubKey = "<RSAKeyValue><Modulus>sGbvxwdlDbqFXOMlVUnAF5ew0t0WpPW7rFpI5jHQOFkht/326dvh7t74RYeMpjy357NljouhpTLA3a6idnn4j6c3jmPWBkjZndGsPL4Bqm+fwE48nKpGPjkj4q/yzT4tHXBTyvaBjA8bVoCTnu+LiC4XEaLZRThGzIn5KQXKCigg6tQRy0GXE13XYFVz/x1mjFbT9/7dS8p85n8BuwlY5JvuBIQkKhuCNFfrUxBWyu87CFnXWjIupCD2VO/GbxaCvzrRjLZjAngLCMtZbYBALksqGPgTUN7ZM24XbPWyLtKPaXF2i4XRR9u6eTj5BfnLbKAU5PIVfjIS+vNYYogteQ==</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
    String auth = "WyIyNjA5IiwiaWE5b0VFT3Q2eDlNR2FvbHBHK2VOYUZ4bzNjT3h5UkNrMCtiYnhPRSJd";

    LicenseKey license = Key.Activate(auth, RSAPubKey, new ActivateModel(3349, "MTMPW-VZERP-JZVNZ-SCPZM", Helpers.GetMachineCode(), 300, 1));
    if (license == null || !Helpers.IsOnRightMachine(license, true, true)) {
        System.out.println("The license does not work.");
    } else {
        System.out.println("The license is valid!");
        System.out.println("It will expire: " + license.Expires);
    }
}

Deactivation

In order to deactivate a license, we can call the Key.Deactivate method, as shown below. Note, we need an access token with Deactivate permission, so the ones used in earlier examples cannot be used (unless they have this permission).

import io.cryptolens.methods.*;
import io.cryptolens.models.*;

public static void main(String args[]) {
    String auth = "";

    boolean result = Key.Deactivate(auth, new DeactivateModel(3349, "MTMPW-VZERP-JZVNZ-SCPZM", Helpers.GetMachineCode()));
    if (result == true) {
        System.out.println("Deactivation successful.");
    } else {
        System.out.println("Deactivation failed.");
    }
}

The call above is useful when node-locking is used. If it's a floating license, deactivation is not necessary since licenses will be deactivated automatically after a certain period of time. However, to force a deactivation earlier, you can use similar code as above with addition of a boolean flag.

import io.cryptolens.methods.*;
import io.cryptolens.models.*;

public static void main(String args[]) {
    String auth = "";

    boolean result = Key.Deactivate(auth, new DeactivateModel(3349, "MTMPW-VZERP-JZVNZ-SCPZM", Helpers.GetMachineCode(), true));
    if (result == true) {
        System.out.println("Deactivation successful.");
    } else {
        System.out.println("Deactivation failed.");
    }
}

Calling through the license server

If you would like to re-route the requests through our license-server that is installed on the client site, you can specify its url using LicenseServerUrl parameter. All API models expose this parameter.

For example, let's suppose that your client runs the license server on http://10.1.1.6:8080 and you want to call Key.GetKey(). In this case, we first define all parameters for the request and then modify the license server url:

GetKeyModel model = new GetKeyModel(3349, "ICVLD-VVSZR-ZTICT-YKGXL");
model.LicenseServerUrl = "http://10.1.1.6:8080";

We do this because there is currently no overload method that accepts LicenseServerUrl parameter.

The entire code is shown below:

String RSAPubKey = "<RSAKeyValue><Modulus>sGbvxwdlDbqFXOMlVUnAF5ew0t0WpPW7rFpI5jHQOFkht/326dvh7t74RYeMpjy357NljouhpTLA3a6idnn4j6c3jmPWBkjZndGsPL4Bqm+fwE48nKpGPjkj4q/yzT4tHXBTyvaBjA8bVoCTn+LiC4XEaLZRThGzIn5KQXKCigg6tQRy0GXE13XYFVz/x1mjFbT9/7dS8p85n8BuwlY5JvuBIQkKhuCNFfrUxBWyu87CFnXWjIupCD2VO/GbxaCvzrRjLZjAngLCMtZbYBALksqGPgTUN7ZM24XbPWyLtKPaXF2i4XRR9u6eTj5BfnLbKAU5PIVfjIS+vNYYogteQ==<Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";

String auth = APIKey.get("getkeyactivate");

APIError error = new APIError();

GetKeyModel model = new GetKeyModel(3349, "ICVLD-VVSZR-ZTICT-YKGXL");
model.LicenseServerUrl = "http://10.1.1.6:8080";

LicenseKey license = Key.GetKey(auth, RSAPubKey, model , error);

if (license == null) {
    System.out.println("The license does not work.");
    System.out.println("Error: " + error.message);
} else {
    System.out.println("The license is valid!");
    System.out.println("It will expire: " + license.Expires);
}