/// <summary> /// This method saves all information inside key information into a file. /// </summary> /// <param name="keyInformation">The key infromation that should be saved into a file</param> /// <param name="file">The entire path including file name, i.e. c:\folder\file.txt</param> public static void SaveKeyInformationToFile(KeyInformation keyInformation, string file) { var bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); var fs = new System.IO.FileStream(file, System.IO.FileMode.OpenOrCreate); bf.Serialize(fs, keyInformation); fs.Close(); }
/// <summary> /// Checks that the Key Information object is valid (in the correct format). You can always add constraints /// such as @<see cref="HasNotExpired"/>. /// </summary> /// <param name="keyInformation"></param> /// <returns>Returns true if the object is valid and false otherwise.</returns> public static bool IsValid(this KeyInformation keyInformation) { if (keyInformation != null) { return(true); } return(false); }
/// <summary> /// Checks so that the machine code corresponds to the machine code of this computer. /// </summary> /// <param name="keyInformation"></param> /// <param name="hashFunction">A hash function used to hash the current computer's parameters.</param> /// <returns></returns> public static KeyInformation IsOnRightMachine(this KeyInformation keyInformation, Func <string, string> hashFunction) { if (keyInformation != null && SKM.getMachineCode(hashFunction) .Equals(keyInformation.Mid)) { return(keyInformation); } return(null); }
/// <summary> /// This method loads key information stored in a file into a key information variable. /// </summary> /// <param name="file">The entire path including file name, i.e. c:\folder\file.txt</param> /// <returns></returns> public static KeyInformation LoadKeyInformationFromFile(string file) { var bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); var fs = new System.IO.FileStream(file, System.IO.FileMode.Open); KeyInformation keyInfo = (KeyInformation)bf.Deserialize(fs); fs.Close(); return(keyInfo); }
/// <summary> /// Checks so that a certain Feature is disabled (i.e. it's set to FALSE). /// </summary> /// <param name="keyInformation"></param> /// <param name="featureNumber">The feature number, eg. feature1, feature 2, etc. FeatureNumber can be 1,2,...,8.</param> /// <returns>A key information object if the condition is satisfied. Null otherwise.</returns> public static KeyInformation HasNotFeature(this KeyInformation keyInformation, int featureNumber) { if (keyInformation != null && featureNumber <= 8 && featureNumber >= 1 && !keyInformation.Features[featureNumber - 1]) { return(keyInformation); } return(null); }
/// <summary> /// Save the current object into a file. It can be read using @<see cref="LoadFromFile"/>. /// </summary> /// <param name="keyInformation"></param> /// <param name="file">The entire path including file name, i.e. c:\folder\file.txt</param> /// <param name="json">If the file is stored in JSON (eg. an activation file with .skm extension), set this parameter to TRUE.</param> /// <returns>A key information object if the condition is satisfied. Null otherwise.</returns> /// <remarks>This method does not use the same JSON format structure as activation files. Instead, /// if you want to read these files using <see cref="LoadFromFile"/>, then activationFile has /// to be set to FALSE.</remarks> public static KeyInformation SaveToFile(this KeyInformation keyInformation, string file = "", bool json = false) { if (keyInformation != null) { if (SKGL.SKM.SaveKeyInformationToFile(keyInformation, file, json)) { return(keyInformation); } } return(null); }
/// <summary> /// Checks that the Key Information object is valid (in the correct format). You can always add constraints /// such as @<see cref="HasNotExpired"/>. /// </summary> /// <param name="keyInformation"></param> /// <param name="rsaPublicKey">The public key (RSA). It can be found here: https://serialkeymanager.com/User/Security </param> /// <returns>Returns true if the object is valid and false otherwise.</returns> public static bool IsValid(this KeyInformation keyInformation, string rsaPublicKey) { if (keyInformation != null) { if (!SKGL.SKM.IsKeyInformationGenuine(keyInformation, rsaPublicKey)) { return(false); } return(true); } return(false); }
/// <summary> /// Checks that they key has not expired (i.e. the expire date has not been reached). /// </summary> /// <param name="keyInformation"></param> /// <param name="checkWithInternetTime">If set to true, we will also check that the local /// time (on the client computer) has not been changed (using SKM.TimeCheck). /// </param> /// <returns>A key information object if the condition is satisfied. Null otherwise.</returns> public static KeyInformation HasNotExpired(this KeyInformation keyInformation, bool checkWithInternetTime = false) { if (keyInformation != null) { TimeSpan ts = keyInformation.ExpirationDate - DateTime.Today; if (ts.Days >= 0) { if (checkWithInternetTime && SKGL.SKM.TimeCheck()) { return(null); } return(keyInformation); } } return(null); }
/// <summary> /// Checks that this object has a valid signature, which means that the content has not been altered /// after that it was generated by Serial Key Manager. /// </summary> /// <param name="keyInformation"></param> /// <param name="rsaPublicKey">The public key (RSA). It can be found here: https://serialkeymanager.com/User/Security </param> /// <param name="signatureExpireInterval">If the activation object contains an activation date (when signDate=true), /// this method will check so that no more than "signatureExpirationInterval" days have passed since the last activation. /// </param> /// <returns>A key information object if the condition is satisfied. Null otherwise.</returns> public static KeyInformation HasValidSignature(this KeyInformation keyInformation, string rsaPublicKey, int?signatureExpirationInterval) { if (keyInformation != null) { if (SKGL.SKM.IsKeyInformationGenuine(keyInformation, rsaPublicKey)) { if (signatureExpirationInterval.HasValue && keyInformation.Date.HasValue) { TimeSpan ts = DateTime.Today - keyInformation.Date.Value; if (ts.Days >= signatureExpirationInterval.Value) { return(null); } } return(keyInformation); } } return(null); }
/// <summary> /// This method will interpret the input from the dictionary that was returned through "GetParameters" method, if the action was either "activate" or "validate". /// </summary> /// <param name="parameters">The dictionary array returned in "GetParameters" method</param> /// <returns>A Key Information object</returns> public static KeyInformation GetKeyInformationFromParameters(Dictionary <string, string> parameters) { var ki = new KeyInformation(); if (parameters.ContainsKey("error")) { return(null); } ki.Valid = true; ki.CreationDate = Convert.ToDateTime(parameters["created"]); ki.ExpirationDate = Convert.ToDateTime(parameters["expires"]); ki.SetTime = Convert.ToInt32(parameters["settime"]); ki.TimeLeft = Convert.ToInt32(parameters["timeleft"]); ki.Features = new bool[] { Convert.ToBoolean(parameters["f1"]), Convert.ToBoolean(parameters["f2"]), Convert.ToBoolean(parameters["f3"]), Convert.ToBoolean(parameters["f4"]), Convert.ToBoolean(parameters["f5"]), Convert.ToBoolean(parameters["f6"]), Convert.ToBoolean(parameters["f7"]), Convert.ToBoolean(parameters["f8"]) }; if (parameters.ContainsKey("notes")) { ki.Notes = parameters["notes"]; } if (parameters.ContainsKey("signature")) { ki.Signature = parameters["signature"]; } if (parameters.ContainsKey("newkey")) { ki.NewKey = parameters["newkey"]; } if (parameters.ContainsKey("mid")) { ki.Mid = parameters["mid"]; } return(ki); }
/// <summary> /// This method allows you to check if the key information (creation date, expiration date, etc.) in a request was modified on the way from Serial Key Manager server to the client application. /// </summary> /// <param name="keyInformation">The variable that contains the key information (including the signature)</param> /// <param name="rsaPublicKey">The public key (RSA)</param> /// <returns>True, if no changes were detected. False, otherwise.</returns> public static bool IsKeyInformationGenuine(KeyInformation keyInformation, string rsaPublicKey) { byte[] data = GetBytes(keyInformation.Valid.ToString() + keyInformation.CreationDate.ToString("yyyy-MM-dd") + keyInformation.ExpirationDate.ToString("yyyy-MM-dd") + keyInformation.SetTime.ToString() + keyInformation.TimeLeft.ToString() + keyInformation.Features[0].ToString() + keyInformation.Features[1].ToString() + keyInformation.Features[2].ToString() + keyInformation.Features[3].ToString() + keyInformation.Features[4].ToString() + keyInformation.Features[5].ToString() + keyInformation.Features[6].ToString() + keyInformation.Features[7].ToString() + (keyInformation.Notes == null ? "": keyInformation.Notes) + (keyInformation.Mid == null ? "" : keyInformation.Mid) ); RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(2048); rsa.FromXmlString(rsaPublicKey); byte[] signature = Convert.FromBase64String(keyInformation.Signature); return(rsa.VerifyData(data, "SHA256", signature)); }
/// <summary> /// Checks that this object has a valid signature, which means that the content has not been altered /// after that it was generated by Serial Key Manager. /// </summary> /// <param name="keyInformation"></param> /// <param name="rsaPublicKey">The public key (RSA). It can be found here: https://serialkeymanager.com/User/Security </param> /// <returns>A key information object if the condition is satisfied. Null otherwise.</returns> public static KeyInformation HasValidSignature(this KeyInformation keyInformation, string rsaPublicKey) { return(HasValidSignature(keyInformation, rsaPublicKey, null)); }
/// <summary> /// Checks so that the machine code corresponds to the machine code of this computer. /// The default hash function is SHA1. /// </summary> /// <param name="keyInformation"></param> /// <returns></returns> public static KeyInformation IsOnRightMachine(this KeyInformation keyInformation) { return(IsOnRightMachine(keyInformation, SKM.getSHA1)); }
/// <summary> /// Load a saved object from file (using @<see cref="SaveToFile"/>). /// </summary> /// <param name="keyInformation"></param> /// <param name="file">The entire path including file name, i.e. c:\folder\file.txt</param> /// <param name="json">If the file is stored in JSON (eg. an activation file with .skm extension), set this parameter to TRUE.</param> /// <param name="activationFile">If you obtained this file from an Activation Form (.skm extension), this should be set to true.</param> /// <returns>A key information object if the condition is satisfied. Null otherwise.</returns> public static KeyInformation LoadFromFile(this KeyInformation keyInformation, string file = "", bool json = false, bool activationFile = false) { return(SKGL.SKM.LoadKeyInformationFromFile(file, json, activationFile)); }
/// <summary> /// Load a saved object from file (using @<see cref="SaveToFile"/>). /// </summary> /// <param name="keyInformation"></param> /// <param name="file">The entire path including file name, i.e. c:\folder\file.txt</param> /// <returns>A key information object if the condition is satisfied. Null otherwise.</returns> public static KeyInformation LoadFromFile(this KeyInformation keyInformation, string file = "") { return(LoadFromFile(keyInformation, file, json: false, activationFile: false)); }
/// <summary> /// Save the current object into a file. It can be read using @<see cref="LoadFromFile"/>. /// </summary> /// <param name="keyInformation"></param> /// <param name="file">The entire path including file name, i.e. c:\folder\file.txt</param> /// <returns>A key information object if the condition is satisfied. Null otherwise.</returns> public static KeyInformation SaveToFile(this KeyInformation keyInformation, string file = "") { return(SaveToFile(keyInformation, file, json: false)); }