private bool CheckIfActivated() { try { if (!KeyHelper.MatchCurrentHardwareId(HWID_)) { log.Error("HWID changed"); return(false); } KeyValidator keyVal = new KeyValidator(Tmpl_); keyVal.SetKey(LicenseKey_); keyVal.SetValidationData("Email", Email_); // the key will not be valid if you set a different user name than the one you have set at key generation LicensingClient licensingClient = new LicensingClient(Tmpl_, LicenseKey_, keyVal.QueryValidationData(null), HWID_, ActivationKey_); if (licensingClient.IsLicenseValid()) { byte[] featureSet = keyVal.QueryKeyData("FeatureSet"); featureSet.ToString(); return(true); } else { return(false); } } catch (Exception E) { log.Error("Program is not activated", E); return(false); } }
public static void Main(string[] args) { var p = new OptionSet() { { "name=", "the name of the person requesting the license", v => _name = v }, // Mandatory { "email=", "the email address of the person requesting the license", v => _email = v }, // Mandatory { "company=", "the company name of the person requesting the license", v => _company = v }, // Mandatory { "expiry=", "the license expiry date in yyyy-mm-dd format", v => _expires = v }, // Mandatory { "?|help", "display this help and exit", v => _showHelp = true }, // Optional }; // Process the command-line arguments List <string> options; try { options = p.Parse(args); } catch (OptionException e) { System.Console.Write("keygen: "); System.Console.WriteLine(e.Message); System.Console.WriteLine("Try `keygen --help' for more information."); return; } // Command line option "--help" if (_showHelp) { ShowHelp(p); return; } // Check that the mandatory input arguments were specified. if (_name == null || _email == null || _company == null || _expires == null) { Console.WriteLine("You did not specify all the arguments. Try `keygen --help' for more information."); return; } /* ---------- PROCESS EXPIRY DATE ---------- */ // Parse the input date. try { _expiryDate = DateTime.ParseExact(_expires, "yyyy-MM-dd", CultureInfo.InvariantCulture); } catch { Console.WriteLine("Invalid expiry date format. Date must be in yyyy-mm-dd format."); return; } // If the expiry date is in the past, then throw an error. DateTime currentDate = DateTime.Now; if (_expiryDate.CompareTo(currentDate) == -1) { // -1 is earlier Console.WriteLine("Expiry date cannot be in the past."); return; } /* ---------- PRIVATE KEY ---------- */ string privateKeyFile = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\private.key"; if (File.Exists(privateKeyFile) == false) { Console.WriteLine("Could not find \"private.key\". This file is needed to generate a license key."); return; } string privateKey = File.ReadAllText(privateKeyFile); /* ---------- CREATE LICENSE TEMPLATE ---------- */ // This is mostly a cut-and-paste from \SoftActivate Licensing SDK Standard\samples\CS\KeyGenCS // Note that the template will have to be duplicated in the C++ code that checks the key. // This is a demo key. Replace this key with your purchased key. SDKRegistration.SetLicenseKey("SLDMX-3GK7N-NRZKM-5SECA-TV25K"); // The license key template specifies what the license key will look like, what data will it contain and // how secure the license key will be. KeyTemplate tmpl = new KeyTemplate(); tmpl.NumberOfGroups = 6; tmpl.CharactersPerGroup = 5; tmpl.Encoding = LicenseKeyEncoding.Base32X; tmpl.GroupSeparator = "-"; tmpl.DataSize = 30; // the license keys will hold 30 bits of data tmpl.SignatureSize = 120; // 120 bits = ((characters per group * number of groups) * bits per character) - //data size ; 120 = ((6 * 5) * 5) - 30 tmpl.ValidationDataSize = 100 * 8; // If the field size (as specified in the template) is smaller than the // data size, the data is truncated to fit the field. // Set the private key used to generate the license keys. This is previously obtained by a call to // LicenseKeyTemplate.GenerateSigningKeyPair(). tmpl.SetPrivateKey(privateKey); // TODO: replace demo key "w9Oz5xMOwkYv" in file with real key. tmpl.SetPublicKey("doxzyMNu7n46whM="); // needed to check that the license generated successfully. // Define the data portion of the license key. Keep in mind that the field names are not included in the // license keys. Only the field values are included. // Add a 14-bit field specifying the product expiration date, between 1/1/2010 and 12/31/2041 tmpl.AddDataField("ExpirationDate", // field name LicenseKeyFieldType.Integer, // field is regarded as an integer 14, // field is 14 bits long (4 bits for month, 5 bits for day, 5 bits for year) 0 // field starts at position 0 in license key data bits ); tmpl.AddValidationField("Name", LicenseKeyFieldType.String, 40 * 8, 0); tmpl.AddValidationField("Company", LicenseKeyFieldType.String, 20 * 8, 40); tmpl.AddValidationField("Email", LicenseKeyFieldType.String, 40 * 8, 80); // create a key generator based on the template above KeyGenerator keyGen = new KeyGenerator(tmpl); /* ---------- ADD LICENSE PROPERTIES AND GENERATE KEY ---------- */ // add an expiration date for this license key int packedDate = PackDate(_expiryDate); keyGen.SetKeyData("ExpirationDate", packedDate); keyGen.SetValidationData("Name", _name); keyGen.SetValidationData("Company", _company); keyGen.SetValidationData("Email", _email); // now generate the license key string licenseKey = keyGen.GenerateKey(); /* ---------- CHECK THE GENERATED KEY ---------- */ // validate the generated key. This sequence of code is also used in the actual product to validate the // entered license key KeyValidator keyVal = new KeyValidator(tmpl); keyVal.SetKey(licenseKey); keyVal.SetValidationData("Name", _name); // the key will not be valid if you set a different user name than the one you have set at key generation keyVal.SetValidationData("Company", _company); keyVal.SetValidationData("Email", _email); if (keyVal.IsKeyValid()) { Console.WriteLine("\nThe generated license key is: " + licenseKey); // now read the expiration date from the license key int expirationDate = keyVal.QueryIntKeyData("ExpirationDate"); Console.WriteLine("The expiration date is " + UnpackDate(expirationDate).ToString("MMMM dd, yyyy")); _licenseXml = new XElement("license", new XElement("name", _name), new XElement("company", _company), new XElement("email", _email), new XElement("key", licenseKey), new XElement("expires", _expires) ); Console.WriteLine(_licenseXml.ToString()); } else { Console.WriteLine("Error creating/validating license key !\n"); } return; }
public void ActivationTask(String email, String key) { string activationStatusStr = ""; KeyValidator keyVal = null; try { // validate the generated key. This sequence of code is also used in the actual product to validate the entered license key keyVal = new KeyValidator(Tmpl_); keyVal.SetKey(key); } catch (Exception ex) { OnActivation(false, "Key is incorrect"); log.Error("Key validation failed " + ex.Message); return; } try { keyVal.SetValidationData("Email", email); // the key will not be valid if you set a different user name than the one you have set at key generation LicensingClient licensingClient = new LicensingClient("https://activation.identamaster.com:444", Tmpl_, key, keyVal.QueryValidationData(null), CurrentHWID_, PRODUCT_ID); licensingClient.AcquireLicense(); LicenseKey_ = key; Email_ = email; ActivationKey_ = licensingClient.ActivationKey; HWID_ = CurrentHWID_; // save keys SaveKeys(); if (!licensingClient.IsLicenseValid()) { switch (licensingClient.LicenseStatus) { case LICENSE_STATUS.InvalidActivationKey: activationStatusStr = "invalid activation key"; break; case LICENSE_STATUS.InvalidHardwareId: activationStatusStr = "invalid hardware id"; break; case LICENSE_STATUS.Expired: { // the license expiration date returned by LicenseExpirationDate property is only valid if IsLicenseValid() returns true, // or if IsLicenseValid() returns false and LicenseStatus returns LicenseStatus.Expired DateTime expDate = licensingClient.LicenseExpirationDate; activationStatusStr = "license expired (expiration date: " + expDate.Month + "/" + expDate.Day + "/" + expDate.Year + ")"; } break; default: activationStatusStr = "unknown"; break; } OnActivation(false, activationStatusStr); } else { State = STATE.ACTIVATED; OnActivation(true, ""); return; } } catch (System.Net.WebException ex) { switch (ex.Status) { case System.Net.WebExceptionStatus.ConnectFailure: OnActivation(false, "Couldn't access activation server"); log.Error("Could not connect to server " + ex.Message); break; case System.Net.WebExceptionStatus.ProtocolError: OnActivation(false, "Server didn't validate this key"); log.Error("Key validation failed " + ex.Message); break; default: OnActivation(false, "Network error"); log.Error("Unknown network error " + ex.Message); break; } return; } catch (Exception ex) { OnActivation(false, "Failed"); log.Error("Key validation failed " + ex.Message); return; } }