Exemplo n.º 1
0
        public static string GenerateLicenseBASE64String(LicenseEntity lic, byte[] certPrivateKeyData, SecureString certFilePwd)
        {
            //Serialize license object into XML
            var _licenseObject = new XmlDocument();

            using (var _writer = new StringWriter())
            {
                var _serializer = new XmlSerializer(typeof(LicenseEntity), new Type[] { lic.GetType() });

                _serializer.Serialize(_writer, lic);

                _licenseObject.LoadXml(_writer.ToString());
            }

            //Get RSA key from certificate
            var cert = new X509Certificate2(certPrivateKeyData, certFilePwd);

            var rsaKey = (RSA)cert.PrivateKey;

            //Sign the XML
            SignXML(_licenseObject, rsaKey);

            //Convert the signed XML into BASE64 string
            return(Convert.ToBase64String(Encoding.UTF8.GetBytes(_licenseObject.OuterXml)));
        }
Exemplo n.º 2
0
        public static LicenseEntity ParseLicenseFromBASE64String(Type licenseObjType, string licenseString, byte[] certPubKeyData, out LicenseStatus licStatus)
        {
            licStatus = LicenseStatus.UNDEFINED;

            if (string.IsNullOrWhiteSpace(licenseString))
            {
                licStatus = LicenseStatus.CRACKED;

                return(null);
            }

            var           _licXML = string.Empty;
            LicenseEntity _lic    = null;

            try
            {
                //Get RSA key from certificate
                var cert   = new X509Certificate2(certPubKeyData);
                var rsaKey = (RSA)cert.PublicKey.Key;

                var xmlDoc = new XmlDocument();

                // Load an XML file into the XmlDocument object.
                xmlDoc.PreserveWhitespace = true;
                xmlDoc.LoadXml(Encoding.UTF8.GetString(Convert.FromBase64String(licenseString)));

                // Verify the signature of the signed XML.
                if (VerifyXml(xmlDoc, rsaKey))
                {
                    var nodeList = xmlDoc.GetElementsByTagName("Signature");
                    xmlDoc.DocumentElement.RemoveChild(nodeList[0]);

                    _licXML = xmlDoc.OuterXml;

                    //Deserialize license
                    var _serializer = new XmlSerializer(typeof(LicenseEntity), new Type[] { licenseObjType });
                    using (var _reader = new StringReader(_licXML))
                    {
                        _lic = (LicenseEntity)_serializer.Deserialize(_reader);
                    }

                    licStatus = LicenseStatus.VALID;

                    licStatus = _lic.DoExtraValidation();

                    if (_lic.IsTimeTrial && DateTime.Now > _lic.ValidUntil.AddDays(1))
                    {
                        licStatus = LicenseStatus.TRIALEXPIRED;
                    }
                }
                else
                {
                    licStatus = LicenseStatus.INVALID;
                }
            }
            catch (Exception e)
            {
                licStatus = LicenseStatus.CRACKED;
            }

            return(_lic);
        }