Пример #1
0
        /// <summary>
        /// Generates a valid ProductKey.
        /// </summary>
        /// <remarks>
        ///A valid key should contain 18 digits in 5 parts:
        ///a: 1 random value & odd means isTrial=true, even means isTrail=false.
        ///b: 2~8 machine key
        ///c: 9~13 left days
        ///d: 14~17 version
        ///e: 18 checksum
        ///</remarks>
        public static ProductKey Create(MachineKey machineKey, DateTime expireDate, Version version, bool isTrial)
        {
            if (!machineKey.IsValid || (expireDate - _baseDate).Days > 99999)
            {
                return(null);
            }

            int a = _random.Next(1, 10); //[1, 9]

            if ((isTrial && a % 2 == 0) || (!isTrial && a % 2 == 1))
            {
                a++;
            }

            int b = int.Parse(machineKey.Key.Substring(0, 7));
            int c = expireDate > _baseDate ? (expireDate - _baseDate).Days : 0;

            string abcd   = a.ToString() + machineKey.Key.Substring(0, 7) + c.ToString().PadLeft(5, '0') + version.GetNumber().ToString().PadLeft(4, '0');
            string e      = Utility.GetNumericHash(abcd, 1);
            string result = abcd + e;

            return(new ProductKey(Encode(result)));
        }
Пример #2
0
        public override License GetLicense(LicenseContext context, Type type, object instance, bool allowExceptions)
        {
            //#if DEBUG
            //            return new MyLicense(this, "Developer", DateTime.Now.AddMonths(1), false);
            //#endif
            var machineKey = MachineKey.Create();

            if (context != null && context.UsageMode == LicenseUsageMode.Designtime)
            {
                var productKey = ProductKey.Create(machineKey, DateTime.Now.AddDays(30), Version.GetDefault(), true);
                return(new MyLicense(this, productKey));
            }
            else
            {
                ProductKey  productKey;
                RegistryKey rk       = Registry.CurrentUser.CreateSubKey("SOFTWARE\\CP_NLayer");
                string      keyName  = "pk"; //product key
                var         keyValue = rk.GetValue(keyName) as string;

                if (keyValue == null)
                {
                    // first run
                    productKey = ProductKey.Create(machineKey, DateTime.Now.AddDays(30), Version.GetDefault(), true);
                    rk.SetValue(keyName, productKey.Key);
                }
                else
                {
                    productKey = new ProductKey(keyValue);
                    if (!productKey.IsValid || productKey.MachineKey.Key != machineKey.Key)
                    {
                        productKey = null;
                    }
                }

                return(new MyLicense(this, productKey));
            }
        }