C#實現註冊碼

來源:互聯網
上載者:User
< DOCTYPE html PUBLIC -WCDTD XHTML StrictEN httpwwwworgTRxhtmlDTDxhtml-strictdtd> 開發軟體時,當用到商業用途時,註冊碼與啟用碼就顯得很重要了。現在的軟體破解技術實在在強了,各種國內外大型軟體都有註冊機制,但同時也不斷地被破解。下面發的只是一個常用版本,發出源碼被破就更容易了,但我們學習的是技術。當然也為以後自己的軟體不會被輕易破解。

第一步。根據卷標,CPU序號,產生機器碼

// 取得裝置硬碟的卷標號
        public static string GetDiskVolumeSerialNumber()
        {
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid="d:"");
            disk.Get();
            return disk.GetPropertyValue("VolumeSerialNumber").ToString();
        }

        //獲得CPU的序號
        public static string getCpu()
        {
            string strCpu = null;
            ManagementClass myCpu = new ManagementClass("win32_Processor");
            ManagementObjectCollection myCpuConnection = myCpu.GetInstances();
            foreach (ManagementObject myObject in myCpuConnection)
            {
                strCpu = myObject.Properties["Processorid"].Value.ToString();
                break;
            }
            return strCpu;
        }
        //產生機器碼
        public static string getMNum()
        {
            string strNum = getCpu() + GetDiskVolumeSerialNumber();//獲得24位Cpu和硬碟序號
            string strMNum = strNum.Substring(0, 24);//從產生的字串中取出前24個字元做為機器碼
            return strMNum;
        }
        public static int[] intCode = new int[127];//儲存體金鑰
        public static int[] intNumber = new int[25];//存機器碼的Ascii值
        public static char[] Charcode = new char[25];//儲存機器碼字
        public static void setIntCode()//給數組賦值小於10的數
        {
            for (int i = 1; i < intCode.Length; i++)
            {
                intCode[i] = i % 9;
            }
        }

第二步。根據機器碼 產生註冊碼
        //產生註冊碼      

       public static string getRNum()
        {
            setIntCode();//初始化127位元組
            for (int i = 1; i < Charcode.Length; i++)//把機器碼存入數組中
            {
                Charcode[i] = Convert.ToChar(getMNum().Substring(i - 1, 1));
            }
            for (int j = 1; j < intNumber.Length; j++)//把字元的ASCII值存入一個整數組中。
            {
                intNumber[j] = intCode[Convert.ToInt32(Charcode[j])] + Convert.ToInt32(Charcode[j]);
            }
            string strAsciiName = "";//用於儲存註冊碼
            for (int j = 1; j < intNumber.Length; j++)
            {
                if (intNumber[j] >= 48 && intNumber[j] <= 57)//判斷字元ASCII值是否0-9之間
                {
                    strAsciiName += Convert.ToChar(intNumber[j]).ToString();
                }
                else if (intNumber[j] >= 65 && intNumber[j] <= 90)//判斷字元ASCII值是否A-Z之間
                {
                    strAsciiName += Convert.ToChar(intNumber[j]).ToString();
                }
                else if (intNumber[j] >= 97 && intNumber[j] <= 122)//判斷字元ASCII值是否a-z之間
                {
                    strAsciiName += Convert.ToChar(intNumber[j]).ToString();
                }
                else//判斷字元ASCII值不在以上範圍內
                {
                    if (intNumber[j] > 122)//判斷字元ASCII值是否大於z
                    {
                        strAsciiName += Convert.ToChar(intNumber[j] - 10).ToString();
                    }
                    else
                    {
                        strAsciiName += Convert.ToChar(intNumber[j] - 9).ToString();
                    }
                }
            }
            return strAsciiName;
        }

第三步。檢查註冊狀況,若沒有註冊,可自訂試用       

/// <summary>
        /// 檢查註冊
        /// </summary>
        private void CheckRegist()
        {

                this.btn_reg.Enabled = true;

                  RegistryKey retkey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("software", true).CreateSubKey("wxk").CreateSubKey("wxk.INI");
                foreach (string strRNum in retkey.GetSubKeyNames())//判斷是否註冊
                {
                    if (strRNum == clsTools.getRNum())
                    {
                        thControl(true);
                        return;
                    }
                }
                thControl(false);
                Thread th2 = new Thread(new ThreadStart(thCheckRegist2));
                th2.Start();

          }
        }
      
        /// <summary>
        /// 驗證試用次數
        /// </summary>
        private static void thCheckRegist2()
        {
            MessageBox.Show("您現在使用的是試用版,該軟體可以免費試用3000000次!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            Int32 tLong;
            try
            {
                tLong = (Int32)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\angel", "UseTimes", 0);
                MessageBox.Show("感謝您已使用了" + tLong + "次", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch
            {
                Registry.SetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\angel", "UseTimes", 0, RegistryValueKind.DWord);
                MessageBox.Show("歡迎新使用者使用本軟體", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            tLong = (Int32)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\angel", "UseTimes", 0);
            if (tLong < 3000000)
            {
                int Times = tLong + 1;
                Registry.SetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\angel", "UseTimes", Times);
            }
            else
            {
                MessageBox.Show("試用次數已到", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Application.Exit();
            }
        }

#c#專欄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.