コード例 #1
0
        private bool _IsValid()
        {
            //Dim _a As New methods ' is only here to provide the geteighthashcode method
            try
            {
                if (string.IsNullOrEmpty(m_res))
                {
                    decodeKeyToString();
                    string _decodedHash    = m_res.Substring(0, 9);
                    string _calculatedHash = m_methods.getEightByteHash(m_res.Substring(9, m_res.Length - 9)).ToString().Substring(0, 9);
                    // changed Math.Abs(_res.Substring(0, 17).GetHashCode).ToString.Substring(0, 8)

                    //When the hashcode is calculated, it cannot be taken for sure,
                    //that the same hash value will be generated.
                    //learn more about this issue: http://msdn.microsoft.com/en-us/library/system.object.gethashcode.aspx
                    if (_decodedHash == _calculatedHash)
                    {
                        return(true);
                    }
                    else
                    {
                        //reset the decoded string
                        m_res = "";
                        return(false);
                    }
                }
                else
                {
                    //_res contains decrypted valid key - so this is valid
                    return(true);
                }
            }
            catch (Exception)
            {
                //if something goes wrong, for example, when decrypting,
                //this function will return false, so that user knows that it is unvalid.
                //if the key is valid, there won't be any errors.
                return(false);
            }
        }
コード例 #2
0
        private static int getMachineCode()
        {
            //This code will generate a 5 digits long key, finger print, of the system
            //where this method is being executed. However, that might be changed in the
            //hash function "GetStableHash", by changing the amount of zeroes in
            //MUST_BE_LESS_OR_EQUAL_TO to the one you want to have. Ex 1000 will return
            //3 digits long hash.
            methods m = new methods();

            //string collectedInfo = System.Environment.MachineName;
            string collectedInfo = string.Empty;

            var x = new List <string>();

            //取得CPU ID;//
            ManagementObjectSearcher SearchCPU = new ManagementObjectSearcher(@"SELECT * FROM Win32_Processor");

            foreach (ManagementObject Win32ProcessObj in SearchCPU.Get())
            {
                // 取得CPU 序號
                if (Win32ProcessObj["ProcessorId"] != null)
                {
                    x.Add(Win32ProcessObj["ProcessorId"].ToString());
                }
            }
            x.Sort();
            collectedInfo += string.Join("", x);
            x.Clear();

            //取得Bios UUID;//
            ManagementObjectSearcher SearchSystemProduct = new ManagementObjectSearcher(@"root\CIMV2", "SELECT * FROM Win32_ComputerSystemProduct");

            foreach (ManagementObject SystemObj in SearchSystemProduct.Get())
            {
                if (SystemObj["UUID"] != null)
                {
                    x.Add(SystemObj["UUID"].ToString().Replace("-", ""));
                }
            }
            x.Sort();
            collectedInfo += string.Join("", x);
            x.Clear();

            //取得網卡MAC 碼;//
            //var Name = new List<string>();
            //foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
            //{
            //    // probably need to do some filtering on ni.NetworkInterfaceType here
            //    //collectedInfo += ni.GetPhysicalAddress();
            //    x.Add(ni.GetPhysicalAddress().ToString());
            //    Name.Add(ni.Name);
            //}
            //x.Sort();
            //collectedInfo += x[x.Count - 1];
            //x.Clear();

            ManagementObjectSearcher SearchPhysicalMedia = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");

            foreach (ManagementObject wmi_HD in SearchPhysicalMedia.Get())
            {
                // get the hardware serial no.
                if (wmi_HD["SerialNumber"] == null &&
                    wmi_HD["Removable"] == null &&
                    wmi_HD["Replaceable"] == null)
                {
                    // Don't add
                }
                else
                {
                    //serialNo = wmi_HD["SerialNumber"].ToString();
                    //hotSwap = Convert.ToBoolean(wmi_HD["HotSwappable"]);
                    //Console.WriteLine(hotSwap);
                    x.Add(wmi_HD["SerialNumber"].ToString());
                }
            }
            x.Sort();
            collectedInfo += string.Join("", x);

            //表示都沒有取到設備的識別序號,直接給定一個值;//
            if (collectedInfo.Length <= 0)
            {
                collectedInfo = "productmachine";
            }
            int LessThan = Convert.ToInt32(Math.Pow(10, BaseConfiguration.MachineCodeMaxDigit));

            return(m.getEightByteHash(collectedInfo, LessThan));
        }