Esempio n. 1
0
        /// <summary>
        /// 0 indicate expired, -1 indicate regist version
        /// </summary>
        /// <param name="licenseFile"></param>
        /// <returns></returns>
        internal static DateInfo GetLeftDay(string licenseFile)
        {
            // Get the XML content from the embedded XML public key.
            Stream s      = null;
            string xmlkey = xmlKey;

            DateInfo dateInfo = new DateInfo();

            dateInfo.leftDays = 0;

            // Create an RSA crypto service provider from the embedded
            // XML document resource (the public key).
            CspParameters parms = new CspParameters(1);         // PROV_RSA_FULL

            parms.Flags = CspProviderFlags.UseMachineKeyStore;  // Use Machine store
            foreach (var key in GetKeys(1))
            {
                parms.KeyContainerName = key;
            }
            parms.KeyNumber = 2;                                                                // AT_SIGNATURE
            RSACryptoServiceProvider csp = new RSACryptoServiceProvider(parms);

            // Load the signed XML license file.
            XmlDocument xmldoc = new XmlDocument();

            Assembly entryAssembly = Assembly.GetEntryAssembly();

            if (entryAssembly == null)
            {
                return(dateInfo);
            }

            FileStream licenseStream = File.Open(licenseFile, FileMode.Open);

            if (licenseStream == null)
            {
                return(dateInfo);
            }

            byte[] licenseData = new byte[licenseStream.Length];
            licenseStream.Read(licenseData, 0, licenseData.Length);
            string encryptString = Encoding.ASCII.GetString(licenseData);

            foreach (var key in GetKeys(0))
            {
                foreach (var key1 in DecryptDES(encryptString, key))
                {
                    encryptString = key1;
                }
            }

            StringReader sr = new StringReader(encryptString);

            xmldoc.Load(sr);

            licenseStream.Close();

            // Create the signed XML object.
            SignedXml sxml = new SignedXml(xmldoc);

            try
            {
                // Get the XML Signature node and load it into the signed XML object.
                XmlNode dsig = xmldoc.GetElementsByTagName("Signature",
                                                           SignedXml.XmlDsigNamespaceUrl)[0];
                sxml.LoadXml((XmlElement)dsig);
            }
            catch
            {
                Console.Error.WriteLine("Error: no signature found.");
                return(dateInfo);
            }

            // Verify the signature.
            if (sxml.CheckSignature(csp))
            {
                XmlNode cpuNode = xmldoc.GetElementsByTagName("CPUID")[0];

                string currentCPUInfo = string.Empty;
                foreach (var key in GetKeys(2))
                {
                    foreach (var key1 in EncryptDES(Hardware.GetCpuID(), key))
                    {
                        currentCPUInfo = key1;
                    }
                }

                foreach (string value in getMd5(currentCPUInfo))
                {
                    if (cpuNode.InnerText == value.Trim())
                    {
                        dateInfo.leftDays = -1;
                        return(dateInfo);
                    }
                }
            }

            XmlNode trialDaysNode = xmldoc.GetElementsByTagName("TrialDays")[0];
            XmlNode startDateNode = xmldoc.GetElementsByTagName("StartDate")[0];

            dateInfo.startDate = Convert.ToDateTime(startDateNode.InnerText);
            dateInfo.leftDays  = Convert.ToInt32(trialDaysNode.InnerText);

            return(dateInfo);
        }
Esempio n. 2
0
        internal static void ModifyLeftDay(string licenseFile, DateTime?startDate, int leftDay)
        {
            // Get the XML content from the embedded XML public key.
            Stream s      = null;
            string xmlkey = xmlKey;

            DateInfo dateInfo = new DateInfo();

            dateInfo.leftDays = 0;

            try
            {
                // Create an RSA crypto service provider from the embedded
                // XML document resource (the public key).
                CspParameters parms = new CspParameters(1);             // PROV_RSA_FULL
                parms.Flags = CspProviderFlags.UseMachineKeyStore;      // Use Machine store
                foreach (var key in GetKeys(1))
                {
                    parms.KeyContainerName = key;
                }
                parms.KeyNumber = 2;                                                            // AT_SIGNATURE
                RSACryptoServiceProvider csp = new RSACryptoServiceProvider(parms);

                // Load the signed XML license file.
                XmlDocument xmldoc = new XmlDocument();

                Assembly entryAssembly = Assembly.GetEntryAssembly();
                if (entryAssembly == null)
                {
                    return;
                }

                FileStream licenseStream = File.Open(licenseFile, FileMode.Open);
                if (licenseStream == null)
                {
                    return;
                }

                byte[] licenseData = new byte[licenseStream.Length];
                licenseStream.Read(licenseData, 0, licenseData.Length);
                string encryptString = Encoding.ASCII.GetString(licenseData);
                foreach (var key in GetKeys(0))
                {
                    foreach (var key1 in DecryptDES(encryptString, key))
                    {
                        encryptString = key1;
                    }
                }
                StringReader sr = new StringReader(encryptString);
                xmldoc.Load(sr);

                licenseStream.Close();

                // Create the signed XML object.
                SignedXml sxml = new SignedXml(xmldoc);

                try
                {
                    // Get the XML Signature node and load it into the signed XML object.
                    XmlNode dsig = xmldoc.GetElementsByTagName("Signature",
                                                               SignedXml.XmlDsigNamespaceUrl)[0];
                    sxml.LoadXml((XmlElement)dsig);
                }
                catch
                {
                    Console.Error.WriteLine("Error: no signature found.");
                    return;
                }

                XmlNode trialDaysNode = xmldoc.GetElementsByTagName("TrialDays")[0];
                XmlNode startDateNode = xmldoc.GetElementsByTagName("StartDate")[0];

                trialDaysNode.InnerText = leftDay.ToString();
                if (startDate != null)
                {
                    startDateNode.InnerText = startDate.Value.Date.ToString();
                }

                MemoryStream ms = new MemoryStream();
                xmldoc.Save(ms);

                string encryptResult = string.Empty;
                foreach (var key in GetKeys(2))
                {
                    foreach (var key1 in EncryptDES(Encoding.UTF8.GetString(ms.GetBuffer()), key))
                    {
                        encryptResult = key1;
                    }
                }
                File.WriteAllText(licenseFile, encryptResult);
                ms.Close();
            }
            catch (System.Exception ex)
            {
            }
        }
Esempio n. 3
0
        internal static DateInfo FindFirstRunTime()
        {
            string   encryptTime = string.Empty;
            DateInfo info        = new DateInfo();

            try
            {
                //    LicenseTable licenseTable = new LicenseTable();

                //     encryptTime = licenseTable.GetDateInfo();

                if (!string.IsNullOrEmpty(encryptTime))
                {
                    string confuseString = string.Empty;
                    foreach (var key in GetKeys(0))
                    {
                        foreach (var key1 in DecryptDES(encryptTime, key))
                        {
                            confuseString = key1;
                        }
                    }

                    string data1      = confuseString.Substring(0, 32);
                    string timeString = confuseString.Remove(0, 32);
                    string data2      = timeString.Substring(4, 32);
                    timeString = timeString.Remove(4, 32);
                    string data3 = timeString.Substring(6, 32);

                    timeString = timeString.Remove(6, 32);
                    timeString = timeString.Remove(12, 32);

                    info.startDate = DateTime.Parse(timeString);
                    info.leftDays  = Convert.ToInt32(GetString(data1));
                    info.maxDays   = Convert.ToInt32(GetString(data2));
                    info.version   = GetString(data3);
                }
                else
                {
                    DateTime currentTime   = TimeHelper.DataStandardTime();
                    string   timeString    = currentTime.ToString();
                    string   confuseString = timeString;

                    Version version    = Assembly.GetExecutingAssembly().GetName().Version;
                    string  versionStr = string.Format("{0}{1}{2}", version.Major.ToString("00"),
                                                       version.Minor.ToString("00"), version.Build.ToString("0000"));

                    confuseString = confuseString.Insert(0, GenerateString("030"));      // left days
                    confuseString = confuseString.Insert(4 + 32, GenerateString("030")); // total trial days
                    confuseString = confuseString.Insert(6 + 32 + 32, GenerateString(versionStr));
                    confuseString = confuseString.Insert(12 + 32 + 32 + 32, Guid.NewGuid().ToString("N"));

                    info.startDate = currentTime;
                    info.leftDays  = 30;
                    info.maxDays   = 30;
                    info.version   = versionStr;
                    foreach (string key in GetKeys(3))
                    {
                        foreach (string value in EncryptDES(Hardware.GetCpuID(), key))
                        {
                            info.hardwareId = value;
                        }
                    }

                    foreach (var key in GetKeys(0))
                    {
                        //    foreach (var value in EncryptDES(confuseString, key))
                        //        licenseTable.SetDataInfo(value);
                    }
                }
            }
            catch (Exception ex)
            {
                //    LogEx.Log(ex);
            }

            return(info);
        }