コード例 #1
0
        private void btnCreate_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtRegInfo.Text.Trim()))
            {
                MessageBox.Show(@"注册信息不能为空!", @"提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            string filename = "";

            SystemRegInfo info = new SystemRegInfo();

            info.IsAuthorized = NicholasEncrypt.EncryptUTF8String("HasAuthorized", GetSystemInfo.EncrytionKey);
            info.RegKey       = txtRegInfo.Text.Trim();
            info.StartDate    = dtpStart.Value.ToString();
            info.EndDate      = dtpEnd.Value.ToString();

            SaveFileDialog sfd = new SaveFileDialog();

            sfd.Filter           = @"授权文件|*.leo";
            sfd.FileName         = DateTime.Now.ToString("yyyyMMddhhmmss") + "系统授权";
            sfd.RestoreDirectory = true;
            sfd.CheckPathExists  = true;
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                filename = sfd.FileName;
                XmlHelper.CreateRegInfoXml(filename, info);
                //PiEncryptHelper.EncFile(filename);
                MessageBox.Show(@"生成成功!");
            }
        }
コード例 #2
0
        public static SystemRegInfo GetSystemRegInfo(string path, string root)
        {
            SystemRegInfo data = new SystemRegInfo();

            try
            {
                XmlDocument       doc      = new XmlDocument();
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.IgnoreComments = true;
                XmlReader reader = XmlReader.Create(path, settings);
                doc.Load(reader);
                if (doc.SelectSingleNode(root) == null)
                {
                    return(null);
                }

                XmlNodeList xnl = doc.SelectSingleNode(root).ChildNodes;
                foreach (XmlNode node in xnl)
                {
                    XmlElement x = (XmlElement)node;
                    switch (x.Name)
                    {
                    case "IsAuthorized":
                        data.IsAuthorized = x.InnerText;
                        break;

                    case "RegKey":
                        data.RegKey = x.InnerText;
                        break;

                    case "StartDate":
                        data.StartDate = x.InnerText;
                        break;

                    case "EndDate":
                        data.EndDate = x.InnerText;
                        break;
                    }
                }
                reader.Close();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return(data);
        }
コード例 #3
0
        public static void CreateRegInfoXml(string filename, SystemRegInfo info)
        {
            //创建XmlDocument对象
            XmlDocument xmlDoc = new XmlDocument();
            //XML的声明<?xml version="1.0" encoding="gb2312"?>
            XmlDeclaration xmlSM = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);

            //追加xmldecl位置
            xmlDoc.AppendChild(xmlSM);
            //添加一个名为Gen的根节点
            XmlElement xml = xmlDoc.CreateElement("", "Software", "");

            //追加Gen的根节点位置
            xmlDoc.AppendChild(xml);
            //添加另一个节点,与Gen所匹配,查找<Gen>
            XmlNode gen = xmlDoc.SelectSingleNode("Software");
            //添加一个名为<Zi>的节点
            XmlElement zi = xmlDoc.CreateElement("IsAuthorized");

            //为<Zi>节点的属性
            zi.InnerText = info.IsAuthorized;
            gen.AppendChild(zi);//添加到<Gen>节点中

            zi = xmlDoc.CreateElement("RegKey");
            //为<Zi>节点的属性
            zi.InnerText = info.RegKey;
            gen.AppendChild(zi);//添加到<Gen>节点中

            zi = xmlDoc.CreateElement("StartDate");
            //为<Zi>节点的属性
            zi.InnerText = info.StartDate;
            gen.AppendChild(zi);//添加到<Gen>节点中

            zi = xmlDoc.CreateElement("EndDate");
            //为<Zi>节点的属性
            zi.InnerText = info.EndDate;
            gen.AppendChild(zi);//添加到<Gen>节点中

            //保存好创建的XML文档
            xmlDoc.Save(filename);
        }
コード例 #4
0
        public static void Update(string path, SystemRegInfo info)
        {
            try
            {
                XmlDocument       doc      = new XmlDocument();
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.IgnoreComments = true;
                XmlReader reader = XmlReader.Create(path, settings);
                doc.Load(reader);
                XmlNodeList nodeList = doc.SelectSingleNode("Software").ChildNodes;
                foreach (XmlNode node in nodeList)
                {
                    XmlElement xe = (XmlElement)node;
                    switch (xe.Name)
                    {
                    case "IsAuthorized":
                        xe.InnerText = info.IsAuthorized;
                        break;

                    case "RegKey":
                        xe.InnerText = info.RegKey;
                        break;

                    case "StartDate":
                        xe.InnerText = info.StartDate;
                        break;

                    case "EndDate":
                        xe.InnerText = info.EndDate;
                        break;
                    }
                }
                reader.Close();
                doc.Save(path);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
コード例 #5
0
        private void btnRegister_Click(object sender, EventArgs e)
        {
            try
            {
                string path = txtRegFile.Text.Trim();
                PiEncryptHelper.DecFile(path);
                SystemRegInfo regInfo = XmlHelper.GetSystemRegInfo(path, "Software");
                PiEncryptHelper.EncFile(path);
                if (regInfo == null)
                {
                    MessageBox.Show(@"读取授权文件失败!", @"错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    //DialogResult = DialogResult.OK;
                    RegSuccess = false;
                    return;
                }

                if (regInfo.RegKey != CheckRegInfo.AesLocalKey)
                {
                    MessageBox.Show(@"该授权文件无法在该电脑上激活!", @"提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    //DialogResult = DialogResult.OK;
                    RegSuccess = false;
                    return;
                }

                DateTime endTime = Convert.ToDateTime(regInfo.EndDate);
                if (DateTime.Compare(endTime, DateTime.Now) < 0)
                {
                    MessageBox.Show(@"该授权文件已过期,请重新激活!", @"提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    //DialogResult = DialogResult.OK;
                    RegSuccess = false;
                    return;
                }

                DateTime startTime = Convert.ToDateTime(regInfo.StartDate);
                if (DateTime.Compare(startTime, DateTime.Now) > 0)
                {
                    MessageBox.Show(@"检测到电脑系统时间不是北京时间,请将电脑时间调整到当前北京时间!", @"提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    //DialogResult = DialogResult.OK;
                    RegSuccess = false;
                    return;
                }

                if (File.Exists(_regPath))
                {
                    PiEncryptHelper.DecFile(_regPath);
                    XmlHelper.Update(_regPath, regInfo);
                }
                else
                {
                    XmlHelper.CreateRegInfoXml(_regPath, regInfo);
                }

                RegisterClass.UpdateSubKey("Nicholas", NicholasEncrypt.EncryptUTF8String("0", GetSystemInfo.EncrytionKey));
                RegisterClass.UpdateSubKey("Leo", NicholasEncrypt.EncryptUTF8String("YES", GetSystemInfo.EncrytionKey));
                RegisterClass.UpdateSubKey("NicholasLeo", NicholasEncrypt.EncryptUTF8String("Due", GetSystemInfo.EncrytionKey));

                RegSuccess = true;
                PiEncryptHelper.EncFile(_regPath);

                MessageBox.Show(@"系统成功激活!");
                DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, @"错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
コード例 #6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="isReg"></param>
        /// <param name="errorInfo"></param>
        /// <param name="infoType">1:无法获取注册信息,2:无法在本机注册,3:未授权,4:已过期</param>
        public static void CheckReg(ref bool isReg, out string errorInfo, out int infoType, out bool isTrial)
        {
            infoType  = 0;
            errorInfo = "";
            isTrial   = false;
            try
            {
                int x = 0;
                //系统未注册
                if (GetSoftRegFlg() == "NO")
                {
                    if (GetLoginTimes() < 10)
                    {
                        isTrial = true;
                        int times = GetLoginTimes() + 1;
                        RegisterClass.UpdateSubKey("Nicholas", NicholasEncrypt.EncryptUTF8String(times.ToString(), GetSystemInfo.EncrytionKey));
                    }
                    else
                    {
                        errorInfo = "检测到系统试用次数已使用完!";
                        isReg     = false;
                        infoType  = 5;
                        RegisterClass.UpdateSubKey("NicholasLeo", NicholasEncrypt.EncryptUTF8String("NoDue", GetSystemInfo.EncrytionKey));
                        return;
                    }
                    isReg    = true;
                    infoType = 10;
                    return;
                }

                if (GetSoftDue() != "Due")
                {
                    errorInfo = "检测到系统未授权!";
                    isReg     = false;
                    infoType  = 5;
                    return;
                }


                if (!File.Exists(RegPath))
                {
                    errorInfo = "无法检测到系统注册信息!";
                    isReg     = false;
                    infoType  = 1;
                    return;
                }

                if (!PiEncryptHelper.IsFileEnc(RegPath, ref x))
                {
                    errorInfo = "授权文件出错,请联系管理员!";
                    isReg     = false;
                    infoType  = 1;
                    return;
                }

                PiEncryptHelper.DecFile(RegPath);
                SystemRegInfo regInfo = XmlHelper.GetSystemRegInfo(RegPath, "Software");
                PiEncryptHelper.EncFile(RegPath);
                if (regInfo == null)
                {
                    errorInfo = "无法检测到系统注册信息!";
                    isReg     = false;
                    infoType  = 1;
                    return;
                }
                if (regInfo.RegKey != AesLocalKey)
                {
                    errorInfo = "检测到系统无法在改电脑上使用!";
                    isReg     = false;
                    infoType  = 2;
                    return;
                }

                DateTime endTime = Convert.ToDateTime(regInfo.EndDate);
                if (DateTime.Compare(endTime, DateTime.Now) < 0)
                {
                    errorInfo = "检测到系统注册已过期,请重新激活!";
                    isReg     = false;
                    infoType  = 3;
                    return;
                }

                DateTime startTime = Convert.ToDateTime(regInfo.StartDate);
                if (DateTime.Compare(startTime, DateTime.Now) > 0)
                {
                    errorInfo = "检测到电脑系统时间不是北京时间,请将电脑时间调整到当前北京时间!";
                    isReg     = false;
                    infoType  = 3;
                    return;
                }

                if (NicholasEncrypt.DecryptUTF8String(regInfo.IsAuthorized, GetSystemInfo.EncrytionKey) != "HasAuthorized")
                {
                    errorInfo = "检测到系统未进行授权!";
                    isReg     = false;
                    infoType  = 3;
                    return;
                }
            }
            catch (Exception)
            {
                errorInfo = "授权文件出错,请重试!";
                isReg     = false;
            }
        }