示例#1
0
        /// <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?.Signature != "")
            {
                var prevSignature = licenseKey.Signature;

                try
                {
                    licenseKey.Signature = "";
                    var rawResult = licenseKey.AsDictionary();
                    RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(2048);

                    rsa.FromXmlString(rsaPublicKey);

                    byte[] signature = Convert.FromBase64String(prevSignature);

                    // the signature should not be included into the signature :)
                    System.Diagnostics.Debug.WriteLine(String.Join(",", rawResult.Select(x => x.Value)));
                    return(rsa.VerifyData(HelperMethods.GetBytes(String.Join(",", rawResult.Select(x => x.Value))), "SHA256", signature));
                }
                catch { }
                finally
                {
                    licenseKey.Signature = prevSignature;
                }
            }

            return(false);
        }
示例#2
0
        /// <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);
        }