Class SKM
This class contains additional methods to ease serial key validation with Cryptolens.
Most of the methods will Web API 2, where "uid", "pid" and "hsum" are required in each request.
These can be found here https://app.cryptolens.io/docs/api/v2/Activate (please make sure you are logged in).
In addition to this, you need to explicitly set each product to be IsPublic and, for some methods,
enable the functionality on your Security page (https://app.cryptolens.io/User/Security).
RSA public keys and your private key can also be found on the Security page.
For Web API 3, you only need one token. You can find information on how it is generated here:
https://app.cryptolens.io/docs/api/v3/Auth.
Inherited Members
Namespace: SKGL
Assembly: Cryptolens.Licensing.CrossPlatform.dll
Syntax
public static class SKM
Remarks
In Debug mode, the error is going to be displayed in the Output Window.
Methods
| Edit this page View SourceDaysLeft(KeyInformation)
Returns the number of days left for a given license (time left). This method is particularly useful when KeyInfo is not updated regularly, because TimeLeft will not be affected (stay constant). If your implementation checks the license with the server periodically, this method should be used instead of TimeLeft.
Declaration
[Obsolete]
public static int DaysLeft(KeyInformation keyInfo)
Parameters
| Type | Name | Description |
|---|---|---|
| KeyInformation | keyInfo |
Returns
| Type | Description |
|---|---|
| int |
DaysLeft(KeyInformation, bool)
Returns the number of days left for a given license (time left). This method is particularly useful when KeyInfo is not updated regularly, because TimeLeft will not be affected (stay constant). If your implementation checks the license with the server periodically, this method should be used instead of TimeLeft.
Declaration
[Obsolete]
public static int DaysLeft(KeyInformation keyInfo, bool zeroIfExpired = false)
Parameters
| Type | Name | Description |
|---|---|---|
| KeyInformation | keyInfo | |
| bool | zeroIfExpired | If true, when a license has expired, zero will be returned. |
Returns
| Type | Description |
|---|---|
| int |
GenerateKey()
Declaration
public static string GenerateKey()
Returns
| Type | Description |
|---|---|
| string |
GetActivatedMachines(ProductVariables, string, string)
This method will retrieve the list of activated machines for a given serial key.
If successful, a list of type ActivatedData will be returned. If an error occurs, null is returned.
Declaration
[Obsolete]
public static List<ActivationData> GetActivatedMachines(ProductVariables productVariables, string privateKey, string sid)
Parameters
| Type | Name | Description |
|---|---|---|
| ProductVariables | productVariables | The object that contains Uid, Pid and Hsum |
| string | privateKey | The private key of the user. |
| string | sid | Serial Key that is to be validated |
Returns
| Type | Description |
|---|---|
| List<ActivationData> | A list of ActivatedData or null. |
GetKeyInformationFromParameters(Dictionary<string, string>)
This method will interpret the input from the dictionary that was returned through "GetParameters" method, if the action was either "activate" or "validate".
Declaration
public static KeyInformation GetKeyInformationFromParameters(Dictionary<string, string> parameters)
Parameters
| Type | Name | Description |
|---|---|---|
| Dictionary<string, string> | parameters | The dictionary array returned in "GetParameters" method |
Returns
| Type | Description |
|---|---|
| KeyInformation | A Key Information object |
GetParameters(Dictionary<string, string>, string)
This method will take in a set of parameters (input parameters) and send them to the given action. You can find them here: https://app.cryptolens.io/docs/api.
Declaration
public static Dictionary<string, string> GetParameters(Dictionary<string, string> inputParameters, string typeOfAction)
Parameters
| Type | Name | Description |
|---|---|---|
| Dictionary<string, string> | inputParameters | A dictionary that contains data such as "uid", "pid", etc. |
| string | typeOfAction | A string that tells what to do, i.e. "validate", "activate" etc. |
Returns
| Type | Description |
|---|---|
| Dictionary<string, string> | A dictionary of the JSON elements returned for that particular request. |
Examples
If you would like to access a method in the Web API manually, please use GetParameters method. A list of them can be found at https://app.cryptolens.io/docs/api.
public void GetParamtersExample()
{
var input = new System.Collections.Generic.Dictionary<string, string >();
input.Add("uid", "1");
input.Add("pid", "1");
input.Add("hsum", "11111");
input.Add("sid", "ABCD-EFGHI-GKLMN-OPQRS");
input.Add("sign","true");
var result = SKGL.SKM.GetParameters(input, "Validate");
var keyinfo = SKGL.SKM.GetKeyInformationFromParameters(result);
if(result.ContainsKey("error") && result["error"] != "")
{
// if we are here, something went wrong.
}
}
|
Edit this page
View Source
GetProductVariables(string, string, string)
Returns the product variables such as "uid", "pid", and "hsum".
Declaration
public static ProductVariables GetProductVariables(string username, string password, string productID)
Parameters
| Type | Name | Description |
|---|---|---|
| string | username | Your username |
| string | password | Your password |
| string | productID | The desired product ID |
Returns
| Type | Description |
|---|---|
| ProductVariables | The "uid","pid", and "hsum" variables |
Examples
This will get pid, uid and hsum.
public void GetProductVariables()
{
var listOfProducts = SKGL.SKM.ListUserProducts("username", "password");
//variables needed in for instance validation/activation
//note, First requires System.Linq.
var productVar = SKGL.SKM.GetProductVariables("username","password", listOfProducts.First().Value);
Debug.WriteLine("The uid=" + productVar.UID + ", pid=" + productVar.PID + " and hsum=" + productVar.HSUM);
}
|
Edit this page
View Source
IsKeyInformationGenuine(KeyInformation, string)
This method allows you to check if the key information (creation date, expiration date, etc.) in a request was modified on the way from Cryptolens server to the client application.
Declaration
public static bool IsKeyInformationGenuine(KeyInformation keyInformation, string rsaPublicKey)
Parameters
| Type | Name | Description |
|---|---|---|
| KeyInformation | keyInformation | The variable that contains the key information (including the signature) |
| string | rsaPublicKey | The public key (RSA) |
Returns
| Type | Description |
|---|---|
| bool | True, if no changes were detected. False, otherwise. |
Examples
The code below demonstrates how IsKeyInformationGenueine can be used in offine key validation. Please read more about offline key validation at https://help.cryptolens.io/examples/offline-verification.
public static void OfflineKeyValidationWithPeriodicTimeCheck()
{
var RSAPublicKey = "RSA public key";
var keyInfo = new KeyInformation().LoadFromFile("license2.txt");
if (keyInfo.HasValidSignature(RSAPublicKey, 30)
.IsOnRightMachine()
.IsValid())
{
// the signature is correct so
// the program can now launch
}
else
{
var machineCode = SKGL.SKM.getMachineCode(SKGL.SKM.getSHA1);
keyInfo = SKGL.SKM.KeyActivation("3", "2", "751963", "MJAWL-ITPVZ-LKGAN-DLJDN", machineCode, secure: true, signMid: true, signDate: true);
if (keyInfo.HasValidSignature(RSAPublicKey)
.IsOnRightMachine()
.IsValid())
{
// the signature is correct and the key is valid.
// save to file.
keyInfo.SaveToFile("license2.txt");
// the program can now launch
}
else
{
// failure. close the program.
}
}
}
|
Edit this page
View Source
KeyActivation(string, string, string, string, string, bool, bool, bool, bool, bool)
This method will check whether the key is valid or invalid against the Cryptolens database.
The method will return an object (KeyInformation) only if:
- the key exists in the database (it has been generated)
- the key is not blocked
- the machine code that is activated has not been activated before
- the limit for maximum number of machine codes has not been achieved
- the machine code exists in the Allowed Machine codes.
NOTE: In Addition, depending on the settings, this method will activate a machine code.
Declaration
[Obsolete("Please use Key.Activate in SKM.V3.Methods.")]
public static KeyInformation KeyActivation(string pid, string uid, string hsum, string sid, string mid, bool secure = false, bool signMid = false, bool signPid = false, bool signUid = false, bool signDate = false)
Parameters
| Type | Name | Description |
|---|---|---|
| string | pid | pid |
| string | uid | uid |
| string | hsum | hsum |
| string | sid | Serial Key that is to be validated |
| string | mid | Machine code |
| bool | secure | If true, the key information will contain a signature of itself that you can validate with IsKeyInformationGenuine |
| bool | signMid | if set to true, the mid parameter will be included into the signature (requires secure to be true). (Note, secure has to be true, since otherwise machine code (mid) will not be included into the signature.) |
| bool | signPid | If true, the Key Information object will contain the Pid field. (Note, secure has to be true, since otherwise Pid will not be included into the signature.) |
| bool | signUid | If true, the Key Information object will contain the Uid field. (Note, secure has to be true, since otherwise Uid will not be included into the signature.) |
| bool | signDate | If true, the Key Information object will contain the Date field. (when validation was performed). (Note, secure has to be true, since otherwise Date will not be included into the signature.) |
Returns
| Type | Description |
|---|---|
| KeyInformation | Returns a KeyInformation object if all rules were satisfied and null if an error occurred. |
Remarks
In Debug mode, the error is going to be displayed in the Output Window.
Examples
For pid, uid and hsum, please see https://app.cryptolens.io/Ext/Val. You can also retreive them using GetProductVariables(string, string, string). NB: If trial activation is configured, the API can return a new key (read more at https://help.cryptolens.io/web-interface/trial-activation).
public void KeyActivation()
{
var validationResult = SKGL.SKM.KeyActivation("pid", "uid", "hsum", "serial key to validate", "machine code", {sign the data}, {sign machine code});
if (validationResult != null)
{
//valid key
var created = validationResult.CreationDate;
var expires = validationResult.ExpirationDate;
var setTime = validationResult.SetTime;
var timeLeft = validationResult.TimeLeft;
var features = validationResult.Features;
}
else
{
//invalid key
Assert.Fail();
}
}
|
Edit this page
View Source
KeyDeactivation(string, string, string, string, string)
This method will attempt to de-activate a machine code from the given key. If the given machine code was de-activated, KeyInformation confirming the key and the machine code will be returned. If something went wrong, for instance, if the machine code did not exist, null will be returned.
Declaration
[Obsolete("Please use Key.Deactivate in SKM.V3.Methods.")]
public static KeyInformation KeyDeactivation(string pid, string uid, string hsum, string sid, string mid)
Parameters
| Type | Name | Description |
|---|---|---|
| string | pid | pid |
| string | uid | uid |
| string | hsum | hsum |
| string | sid | Machine code's serial key. |
| string | mid | Machine code |
Returns
| Type | Description |
|---|---|
| KeyInformation | Returns a KeyInformation object (with a key and machine code only) or null. |
Remarks
In Debug mode, the error is going to be displayed in the Output Window.
Note: The key is going to be stored in "NewKey" field, while the machine code is going to be stored in "mid".
Examples
The following code demonstrates activation of a machine code followed by its deactivation.
public void KeyDeactivationTest()
{
// first, we need to activate a machine code. In this case, it's "artem123"
var activationResult = SKGL.SKM.KeyActivation("2196", "2", "749172", "KTDOU-JZQUY-NOJCU-ECTAA", "artem123");
if(!activationResult.IsValid())
{
Assert.Fail("Unable to activate");
}
// now, let's deactivate it:
var deactivationResult = SKGL.SKM.KeyDeactivation("2196", "2", "749172", "KTDOU-JZQUY-NOJCU-ECTAA", "artem123");
if(!deactivationResult.IsValid())
{
Assert.Fail("Unable to deactivate");
}
// if we are here, the machine code "artem123" was successfully deactivated.
}
|
Edit this page
View Source
KeyValidation(string, string, string, string, bool, bool, bool, bool)
This method will check whether the key is valid or invalid against the Cryptolens database
The method will return an object (KeyInformation) only if:
- the key exists in the database (it has been generated)
- the key is not blocked
Declaration
[Obsolete]
public static KeyInformation KeyValidation(string pid, string uid, string hsum, string sid, bool secure = false, bool signPid = false, bool signUid = false, bool signDate = false)
Parameters
| Type | Name | Description |
|---|---|---|
| string | pid | pid |
| string | uid | uid |
| string | hsum | hsum |
| string | sid | Serial Key that is to be validated |
| bool | secure | If true, the Key Information will contain a signature of itself that you can validate with IsKeyInformationGenuine |
| bool | signPid | If true, the Key Information object will contain the Pid field. (Note, secure has to be true, since otherwise Pid will not be included into the signature.) |
| bool | signUid | If true, the Key Information object will contain the Uid field. (Note, secure has to be true, since otherwise Uid will not be included into the signature.) |
| bool | signDate | If true, the Key Information object will contain the Date field. (when validation was performed). (Note, secure has to be true, since otherwise Date will not be included into the signature.) |
Returns
| Type | Description |
|---|---|
| KeyInformation | KeyInformation or null. |
Remarks
In Debug mode, the error is going to be displayed in the Output Window.
Examples
For pid, uid and hsum, please see https://app.cryptolens.io/Ext/Val. You can retreive them using GetProductVariables(string, string, string).
public void KeyValidation()
{
var validationResult = SKGL.SKM.KeyValidation("pid", "uid", "hsum", "serial key to validate", "machine code", {sign the data}, {sign machine code});
if (validationResult.IsValid())
{
//valid key
var created = validationResult.CreationDate;
var expires = validationResult.ExpirationDate;
var setTime = validationResult.SetTime;
var timeLeft = validationResult.TimeLeft;
var features = validationResult.Features;
}
else
{
//invalid key
Assert.Fail();
}
}
|
Edit this page
View Source
ListUserProducts(string, string)
Lists all your products associated with your account. Each product name is accompanied with a product id.
Declaration
public static Dictionary<string, string> ListUserProducts(string username, string password)
Parameters
| Type | Name | Description |
|---|---|---|
| string | username | Your username |
| string | password | Your password |
Returns
| Type | Description |
|---|---|
| Dictionary<string, string> | All products as a dictionary. The "key" is the product name and the "value" is the product id. |
LoadKeyInformationFromFile(string, bool, bool)
This method loads key information stored in a file into a key information variable.
Declaration
public static KeyInformation LoadKeyInformationFromFile(string file, bool json = false, bool activationFile = false)
Parameters
| Type | Name | Description |
|---|---|---|
| string | file | The entire path including file name, i.e. c:\folder\file.txt |
| bool | json | If the file is stored in JSON (eg. an activation file with .skm extension), set this parameter to TRUE. |
| bool | activationFile | If you obtained this file from an Activation Form (.skm extension), this should be set to true. |
Returns
| Type | Description |
|---|---|
| KeyInformation | If successful, this method returns a KeyInformation object. Null otherwise. |
Remarks
If you want to read a file that uses the JSON format created by SaveKeyInformationToFile(KeyInformation, string, bool), activationFile has to be set to FALSE while json is set to TRUE.
LoadProductVariablesFromString(string)
This method will load ProductVariables data from a json serialized string. (see Example below.)
Declaration
public static ProductVariables LoadProductVariablesFromString(string productVariablesString)
Parameters
| Type | Name | Description |
|---|---|---|
| string | productVariablesString | The json version of Product Variables, i.e. {"uid":"111", "pid":"111", "hsum":"111"} |
Returns
| Type | Description |
|---|---|
| ProductVariables |
Examples
An example of a string that contains the the serialized json string is shown below:
public void LoadProductVariablesFromString()
{
var productVariables = SKGL.SKM.LoadProductVariablesFromString("{\"pid\":\"test\", \"uid\":\"test1\", \"hsum\":\"test2\"}");
Assert.AreEqual(productVariables.PID, "test");
Assert.AreEqual(productVariables.UID, "test1");
Assert.AreEqual(productVariables.HSUM, "test2");
}
|
Edit this page
View Source
OptionalField(ProductVariables, string, Todo, int)
Declaration
public static int OptionalField(ProductVariables productVariables, string sid, SKM.Todo todo = Todo.Get, int decrement = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| ProductVariables | productVariables | The object that contains Uid, Pid and Hsum |
| string | sid | Serial Key that is to be validated |
| SKM.Todo | todo | Action to perform. Either Get or Set. |
| int | decrement | If |
Returns
| Type | Description |
|---|---|
| int | An intger that is currently stored in the optional field. |
Examples
The code below first checks the value of the optional field and then decreases it by 1. The Assert.True will be true in this case.
public void TestOptionalField()
{
// let's assume that the following key has an optional field of the value 5.
// edit: this will pass several thousand times. then, it has to be increased again.
var productVariables = new SKGL.ProductVariables() { UID = "2", PID = "2196", HSUM = "749172" };
int currentvalue = SKGL.SKM.OptionalField(productVariables, "KTDOU-JZQUY-NOJCU-ECTAA");
int newValue = SKGL.SKM.OptionalField(productVariables, "KTDOU-JZQUY-NOJCU-ECTAA", SKGL.SKM.Todo.Set, 1);
Assert.IsTrue(newValue == currentvalue - 1);
}
|
Edit this page
View Source
SaveKeyInformationToFile(KeyInformation, string, bool)
This method saves all information inside key information into a file.
Declaration
public static bool SaveKeyInformationToFile(KeyInformation keyInformation, string file, bool json = false)
Parameters
| Type | Name | Description |
|---|---|---|
| KeyInformation | keyInformation | The key infromation that should be saved into a file |
| string | file | The entire path including file name, i.e. c:\folder\file.txt |
| bool | json | Save the file using JSON format. |
Returns
| Type | Description |
|---|---|
| bool | If successful, true will be returned. False otherwise. |
Remarks
This method does not use the same JSON format structure as activation files. Instead, if you want to read these files using LoadKeyInformationFromFile(string, bool, bool), then activationFile has to be set to FALSE.
TimeCheck()
This method checks whether the network time is different from the local time (client computer). This helps to prevent date changes caused by a client.
Declaration
public static bool TimeCheck()
Returns
| Type | Description |
|---|---|
| bool | Returns FALSE if time was NOT changed and TRUE if the time was changed. |
Examples
The following code demonstrances the way TimeCheck can be used.
public void HasLocalTimeChanged()
{
bool hasChanged = SKGL.SKM.TimeCheck();
if(hasChanged)
{
Debug.WriteLine("The local time was changed by the user. Validation fails.");
}
else
{
Debug.WriteLine("The local time hasn't been changed. Continue validation.");
}
}
|
Edit this page
View Source
getEightDigitsLongHash(string)
This method will generate an 8 digit long hash which can be stored as an Int32.
Declaration
public static string getEightDigitsLongHash(string s)
Parameters
| Type | Name | Description |
|---|---|---|
| string | s | The string value of the infromation that is to be hashed. |
Returns
| Type | Description |
|---|---|
| string | A string with the hash value |
Remarks
Please see getMachineCode(Func<string, string>) for a code example of how this method can be used.
getMachineCode(Func<string, string>)
Declaration
public static string getMachineCode(Func<string, string> hashFunction)
Parameters
| Type | Name | Description |
|---|---|---|
| Func<string, string> | hashFunction |
Returns
| Type | Description |
|---|---|
| string |
getMachineCode(Func<string, string>, bool)
This method will calculate a machine code
Declaration
public static string getMachineCode(Func<string, string> hashFunction, bool includeUserName = false)
Parameters
| Type | Name | Description |
|---|---|---|
| Func<string, string> | hashFunction | The hash function that is to be used. getEightDigitLongHash or SHA1 can be used as a default hash function. |
| bool | includeUserName | If set to TRUE, the user name of the current user will be be taken into account int he signature (.NET Framework only). |
Returns
| Type | Description |
|---|---|
| string | A machine code |
Remarks
On platforms other than .NET Framework 4.0 and 4.6, includeUserName value will be false.
Examples
Machine code can be calculated with the function below. Any other hash algorithm will do, as long as it only contains letters and digits only.
'eg. "61843235" (getEightDigitsLongHash)
'eg. "D38F13CAB8938AC3C393BC111E1A85BB4BA2CCC9" (getSHA1)
Dim machineCode = SKGL.SKM.getMachineCode(AddressOf SKGL.SKM.getEightDigitsLongHash)
Dim machineCode = SKGL.SKM.getMachineCode(AddressOf SKGL.SKM.getSHA1)
//eg. "61843235" (getEightDigitsLongHash)
//eg. "D38F13CAB8938AC3C393BC111E1A85BB4BA2CCC9" (getSHA1)
string machineID1 = SKGL.SKM.getMachineCode(SKGL.SKM.getEightDigitsLongHash);
string machineID2 = SKGL.SKM.getMachineCode(SKGL.SKM.getSHA1);
|
Edit this page
View Source
getSHA1(string)
This method will generate a SHA1 hash.
Declaration
public static string getSHA1(string s)
Parameters
| Type | Name | Description |
|---|---|---|
| string | s | The string value of the infromation that is to be hashed. |
Returns
| Type | Description |
|---|---|
| string | A string with the hash value |
Remarks
Please see getMachineCode(Func<string, string>) for a code example of how this method can be used.
getSHA256(string)
This method will generate a SHA256 hash.
Declaration
public static string getSHA256(string s)
Parameters
| Type | Name | Description |
|---|---|---|
| string | s | The string value of the information that is to be hashed. |
Returns
| Type | Description |
|---|---|
| string | A string with the hash value |
Remarks
Please see getMachineCode(Func<string, string>) for a code example of how this method can be used.
getSHA256(string, int)
This method will generate a SHA256 hash.
Declaration
public static string getSHA256(string s, int v = 1)
Parameters
| Type | Name | Description |
|---|---|---|
| string | s | The string value of the information that is to be hashed. |
| int | v |
Returns
| Type | Description |
|---|---|
| string | A string with the hash value |
Remarks
Please see getMachineCode(Func<string, string>) for a code example of how this method can be used.