Client API to access the functionality of Cryptolens Web API
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.idea
example-app
src
.gitignore
LICENSE
README.md
pom.xml

README.md

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/

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.

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);

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.");
    }
}