⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 framepolice.cs

📁 结构很好的三层数据访问组件。开放源码。是微软企业库数据访问的替代产品之一。
💻 CS
字号:
namespace FrameCountry
{
    using FrameCountry.FrameBuild;
    using System;
    using System.IO;
    using System.Management;
    using System.Runtime.InteropServices;
    using System.Security.Cryptography;
    using System.Text;
    using System.Xml;

    internal class FramePolice
    {
        public bool allowAccess2000 = false;
        public bool allowDB2 = false;
        public bool allowMySQL = false;
        public bool allowOracle = false;
        public bool allowSQLServer = false;
        public bool allowSysbase = false;
        private string ClassAlias = "PO";
        private FrameCity frameCity = new FrameCity();
        private FrameNode frameNode = new FrameNode();
        private const string IV_64 = "1<0R2`5?";
        private const string KEY_64 = "8i3^1$0|";
        private const string serialIV_64 = "X)(i*=ao";
        private const string serialKEY_64 = "X&*ia/?o";

        public bool CheckRegister(string ConnString, ref string ErrorInfo)
        {
            string functionAlias = "00";
            string errorInfo = "";
            string registerString = "";
            try
            {
                if (!this.GetRegisterString(ConnString, ref registerString, ref errorInfo))
                {
                    throw new Exception("◇" + errorInfo);
                }
                int index = registerString.IndexOf("]");
                if (index < 0)
                {
                    throw new Exception("注册码日期错误!");
                }
                DateTime today = DateTime.Today;
                try
                {
                    today = DateTime.Parse(registerString.Substring(1, index - 1));
                }
                catch
                {
                    throw new Exception("分解提醒日期错误!");
                }
                registerString = this.DESDecode(registerString.Substring(index + 1, (registerString.Length - index) - 1));
                if (registerString == null)
                {
                    throw new Exception("注册码格式错误!");
                }
                try
                {
                    XmlDocument document = new XmlDocument();
                    document.LoadXml(registerString);
                    string s = "";
                    if (document.SelectNodes("RegisterInfo/LimitDate").Count > 0)
                    {
                        s = document.SelectSingleNode("RegisterInfo/LimitDate").InnerText;
                        if (today != DateTime.Parse(s))
                        {
                            throw new Exception("限定日期为 " + s + ",提醒日期为 " + today.ToShortDateString() + ",日期不一致!");
                        }
                        if (DateTime.Today > DateTime.Parse(s))
                        {
                            throw new Exception("限定日期为" + s + ",超过限定期限!");
                        }
                    }
                    if ((document.SelectNodes("RegisterInfo/CPUSerialNumber").Count > 0) && (document.SelectSingleNode("RegisterInfo/CPUSerialNumber").InnerText != this.Device_GetCPUSerialNumber()))
                    {
                        throw new Exception("硬件 A 注册信息不符合!");
                    }
                    if ((document.SelectNodes("RegisterInfo/MacAddress").Count > 0) && (document.SelectSingleNode("RegisterInfo/MacAddress").InnerText != this.Device_GetMacAddress()))
                    {
                        throw new Exception("硬件 B 注册信息不符合!");
                    }
                    if ((document.SelectNodes("RegisterInfo/VolumeSerialNumber").Count > 0) && (document.SelectSingleNode("RegisterInfo/VolumeSerialNumber").InnerText != this.Device_GetVolumeSerialNumber("C:")))
                    {
                        throw new Exception("硬件 C 注册信息不符合!");
                    }
                    if ((document.SelectNodes("RegisterInfo/ProductName").Count > 0) && (document.SelectSingleNode("RegisterInfo/ProductName").InnerText != this.System_GetProductName()))
                    {
                        throw new Exception("系统 A 注册信息不符合!");
                    }
                    if ((document.SelectNodes("RegisterInfo/ProductId").Count > 0) && (document.SelectSingleNode("RegisterInfo/ProductId").InnerText != this.System_GetProductId()))
                    {
                        throw new Exception("系统 B 注册信息不符合!");
                    }
                    if ((document.SelectNodes("RegisterInfo/Access2000").Count > 0) && (document.SelectSingleNode("RegisterInfo/Access2000").InnerText == "1"))
                    {
                        this.allowAccess2000 = true;
                    }
                    if ((document.SelectNodes("RegisterInfo/SQLServer").Count > 0) && (document.SelectSingleNode("RegisterInfo/SQLServer").InnerText == "1"))
                    {
                        this.allowSQLServer = true;
                    }
                    if ((document.SelectNodes("RegisterInfo/Oracle").Count > 0) && (document.SelectSingleNode("RegisterInfo/Oracle").InnerText == "1"))
                    {
                        this.allowOracle = true;
                    }
                    if ((document.SelectNodes("RegisterInfo/DB2").Count > 0) && (document.SelectSingleNode("RegisterInfo/DB2").InnerText == "1"))
                    {
                        this.allowDB2 = true;
                    }
                    if ((document.SelectNodes("RegisterInfo/MySQL").Count > 0) && (document.SelectSingleNode("RegisterInfo/MySQL").InnerText == "1"))
                    {
                        this.allowMySQL = true;
                    }
                    if ((document.SelectNodes("RegisterInfo/Sysbase").Count > 0) && (document.SelectSingleNode("RegisterInfo/Sysbase").InnerText == "1"))
                    {
                        this.allowSysbase = true;
                    }
                }
                catch (Exception exception)
                {
                    throw new Exception(exception.Message);
                }
            }
            catch (Exception exception2)
            {
                ErrorInfo = this.frameCity.DealErrorInfo(this.ClassAlias, functionAlias, exception2.Message);
                return false;
            }
            return true;
        }

        public string DESDecode(string data)
        {
            byte[] buffer3;
            byte[] bytes = Encoding.ASCII.GetBytes("8i3^1$0|");
            byte[] rgbIV = Encoding.ASCII.GetBytes("1<0R2`5?");
            try
            {
                buffer3 = Convert.FromBase64String(data);
            }
            catch
            {
                return null;
            }
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            MemoryStream stream = new MemoryStream(buffer3);
            CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(bytes, rgbIV), CryptoStreamMode.Read);
            StreamReader reader = new StreamReader(stream2);
            return reader.ReadToEnd();
        }

        public string DESEncode(string data)
        {
            byte[] bytes = Encoding.ASCII.GetBytes("8i3^1$0|");
            byte[] rgbIV = Encoding.ASCII.GetBytes("1<0R2`5?");
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            int keySize = provider.KeySize;
            MemoryStream stream = new MemoryStream();
            CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(bytes, rgbIV), CryptoStreamMode.Write);
            StreamWriter writer = new StreamWriter(stream2);
            writer.Write(data);
            writer.Flush();
            stream2.FlushFinalBlock();
            writer.Flush();
            return Convert.ToBase64String(stream.GetBuffer(), 0, (int) stream.Length);
        }

        public string Device_GetCPUSerialNumber()
        {
            string str = "";
            ManagementClass class2 = new ManagementClass("Win32_Processor");
            foreach (ManagementObject obj2 in class2.GetInstances())
            {
                str = ";" + str + obj2.Properties["ProcessorId"].Value.ToString();
            }
            return str.Substring(1, str.Length - 1);
        }

        public string Device_GetMacAddress()
        {
            string str = "";
            ManagementClass class2 = new ManagementClass("Win32_NetworkAdapterConfiguration");
            foreach (ManagementObject obj2 in class2.GetInstances())
            {
                if ((bool) obj2["IPEnabled"])
                {
                    str = ";" + str + obj2["MacAddress"].ToString();
                    break;
                }
            }
            return str.Substring(1, str.Length - 1);
        }

        public string Device_GetVolumeSerialNumber(string volumePath)
        {
            ManagementObject obj2 = new ManagementObject("Win32_LogicalDisk.DeviceID=\"" + volumePath + "\"");
            return obj2["VolumeSerialNumber"].ToString();
        }

        private bool GetRegisterString(string ConnString, ref string RegisterString, ref string ErrorInfo)
        {
            string functionAlias = "01";
            string innerText = "";
            string errorInfo = "";
            try
            {
                XmlNode node = this.frameNode.GetNode(ConnString, "Register", ref errorInfo);
                if (node == null)
                {
                    throw new Exception("◇" + errorInfo);
                }
                try
                {
                    innerText = node.SelectNodes("./RegisterString")[0].InnerText;
                }
                catch (Exception exception)
                {
                    throw new Exception("分解 Register 项元素错误!错误信息如下:\n\r" + exception.Message);
                }
            }
            catch (Exception exception2)
            {
                ErrorInfo = this.frameCity.DealErrorInfo(this.ClassAlias, functionAlias, exception2.Message);
                return false;
            }
            RegisterString = innerText;
            return true;
        }

        public string SerialDESDecode(string data)
        {
            byte[] buffer3;
            byte[] bytes = Encoding.ASCII.GetBytes("X&*ia/?o");
            byte[] rgbIV = Encoding.ASCII.GetBytes("X)(i*=ao");
            try
            {
                buffer3 = Convert.FromBase64String(data);
            }
            catch
            {
                return null;
            }
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            MemoryStream stream = new MemoryStream(buffer3);
            CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(bytes, rgbIV), CryptoStreamMode.Read);
            StreamReader reader = new StreamReader(stream2);
            return reader.ReadToEnd();
        }

        public string SerialDESEncode(string data)
        {
            byte[] bytes = Encoding.ASCII.GetBytes("X&*ia/?o");
            byte[] rgbIV = Encoding.ASCII.GetBytes("X)(i*=ao");
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            int keySize = provider.KeySize;
            MemoryStream stream = new MemoryStream();
            CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(bytes, rgbIV), CryptoStreamMode.Write);
            StreamWriter writer = new StreamWriter(stream2);
            writer.Write(data);
            writer.Flush();
            stream2.FlushFinalBlock();
            writer.Flush();
            return Convert.ToBase64String(stream.GetBuffer(), 0, (int) stream.Length);
        }

        public string System_GetProductId()
        {
            string regeditValue = "Machine";
            string errorInfo = "";
            FrameSystem system = new FrameSystem();
            if (system.GetRegeditInfo("machine", @"Software\Microsoft\Windows NT\CurrentVersion", "ProductId", ref regeditValue, ref errorInfo))
            {
                return regeditValue;
            }
            return "";
        }

        public string System_GetProductName()
        {
            OSVersionInfo structure = new OSVersionInfo();
            structure.OSVersionInfoSize = Marshal.SizeOf(structure);
            LibWrap.GetVersionEx(structure);
            switch ((structure.MajorVersion.ToString() + "." + structure.MinorVersion.ToString()))
            {
                case "4.0":
                    switch (structure.PlatformId)
                    {
                        case 1:
                            return "Windows 95";

                        case 2:
                            return "Windows NT 4.0";
                    }
                    return "操作系统未知!";

                case "4.10":
                    return "Windows 98";

                case "4.90":
                    return "Windows Me";

                case "3.51":
                    return "Windows NT 3.51";

                case "5.0":
                    return "Windwos 2000";

                case "5.1":
                    return "Windwos XP";

                case "5.2":
                    return "Windows Server 2003 family";
            }
            return "操作系统未知!";
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -