/// <summary> /// Get the license object as a string. Signature verification will occur automatically. Returns a new license key object. /// </summary> public static LicenseKey LoadFromString(this LicenseKey licenseKey, string RSAPubKey, string serializedLicenseObject) { try { var response = Newtonsoft.Json.JsonConvert.DeserializeObject <RawResponse>(serializedLicenseObject); if (!string.IsNullOrEmpty(response.LicenseKey)) { return(LicenseKey.FromResponse(RSAPubKey, response)); } else { if (RSAPubKey == null) { return(Newtonsoft.Json.JsonConvert.DeserializeObject <LicenseKey>(serializedLicenseObject)); } else { return(Newtonsoft.Json.JsonConvert.DeserializeObject <LicenseKey>(serializedLicenseObject).HasValidSignature(RSAPubKey)); } } } catch (Exception ex) { } return(null); }
/// <summary> /// Checks if the license key file has been modified by an unauthorized person. /// This method will only return true if the license key information is exactly /// the same as the one that was provided and signed by SKM. /// </summary> /// <param name="rsaPublicKey"></param> /// <returns>Returns true if the signature is valid. False otherwise.</returns> private static bool IsLicenceseKeyGenuine(this LicenseKey licenseKey, string rsaPublicKey) { if (licenseKey?.RawResponse != null) { var license = LicenseKey.FromResponse(rsaPublicKey, licenseKey.RawResponse); if (license == null) { return(false); } return(license.Equals(licenseKey)); } if (licenseKey?.Signature != "") { var prevSignature = licenseKey.Signature; try { licenseKey.Signature = ""; var rawResult = licenseKey.AsDictionary(); #if NET40 || NET46 || NET35 || NET47 || NET471 || NET45 RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(2048); rsa.FromXmlString(rsaPublicKey); #else RSA rsa = RSA.Create(); rsa.ImportParameters(SecurityMethods.FromXMLString(rsaPublicKey)); #endif byte[] signature = Convert.FromBase64String(prevSignature); // the signature should not be included into the signature :) #if NET40 || NET46 || NET35 || NET47 || NET471 || NET45 return(rsa.VerifyData(HelperMethods.GetBytes(String.Join(",", rawResult.Where(x => x.Key != "RawResponse").Select(x => x.Value).ToArray())), "SHA256", signature)); #else return(rsa.VerifyData(HelperMethods.GetBytes(String.Join(",", rawResult.Where(x => x.Key != "RawResponse").Select(x => x.Value))), signature, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1)); #endif } catch { } finally { licenseKey.Signature = prevSignature; } } return(false); }