為自己寫的程序加一個注冊功能吧。生成的機器號是根據(jù)CPU和硬盤號來的,根據(jù)自己的需求改成是否是隨機生成。
代碼直接粘貼到新建類覆蓋原代碼就能直接用了。
using System; using System.Management; using System.Security.Cryptography; using System.Text; namespace RegisterClass { class RegisterClass { // 步驟一: 獲得CUP序列號和硬盤序列號的實現(xiàn)代碼如下: // 獲得CPU的序列號 bool Stupids = true ; bool Cat = false ; public 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; } // 取得設(shè)備硬盤的卷標號 public string GetDiskVolumeSerialNumber() { ManagementClass mc = new ManagementClass( " Win32_NetworkAdapterConfiguration " ); ManagementObject disk = new ManagementObject( " win32_logicaldisk.deviceid=\"c:\" " ); disk.Get(); return disk.GetPropertyValue( " VolumeSerialNumber " ).ToString(); } // 步驟二: 收集硬件信息生成機器碼, 代碼如下: // 生成機器碼 public string CreateCode() { string temp = getCpu() + GetDiskVolumeSerialNumber(); // 獲得24位Cpu和硬盤序列號 string [] strid = new string [ 24 ]; // for ( int i = 0 ; i < 24 ; i++) // 把字符賦給數(shù)組 { strid[i] = temp.Substring(i, 1 ); } temp = "" ; // Random rdid = new Random(); for ( int i = 0 ; i < 24 ; i++) // 從數(shù)組隨機抽取24個字符組成新的字符生成機器三 { // temp += strid[rdid.Next(0, 24)]; temp += strid[i+ 3 >= 24 ? 0 :i+ 3 ]; } return GetMd5(temp); } // 步驟三: 使用機器碼生成軟件注冊碼, 代碼如下: // 使用機器碼生成注冊碼 public int [] intCode = new int [ 127 ]; // 用于存密鑰 public void setIntCode() // 給數(shù)組賦值個小于10的隨機數(shù) { // Random ra = new Random(); // for (int i = 1; i < intCode.Length;i++ ) // { // intCode[i] = ra.Next(0, 9); // } for ( int i = 1 ; i < intCode.Length; i++ ) { intCode[i] = i + 3 > 9 ? 0 : i + 3 ; } } public int [] intNumber = new int [ 25 ]; // 用于存機器碼的Ascii值 public char [] Charcode = new char [ 25 ]; // 存儲機器碼字 // 生成注冊碼 public string GetCode( string code) { if (code != "" ) { // 把機器碼存入數(shù)組中 setIntCode(); // 初始化127位數(shù)組 for ( int i = 1 ; i < Charcode.Length; i++) // 把機器碼存入數(shù)組中 { Charcode[i] = Convert.ToChar(code.Substring(i - 1 , 1 )); } // for ( int j = 1 ; j < intNumber.Length; j++) // 把字符的ASCII值存入一個整數(shù)組中。 { intNumber[j] = intCode[Convert.ToInt32(Charcode[j])] + Convert.ToInt32(Charcode[j]); } string strAsciiName = null ; // 用于存儲機器碼 for ( int j = 1 ; j < intNumber.Length; j++ ) { // MessageBox.Show((Convert.ToChar(intNumber[j])).ToString()); // 判斷字符ASCII值是否0-9之間 if (intNumber[j] >= 48 && intNumber[j] <= 57 ) { strAsciiName += Convert.ToChar(intNumber[j]).ToString(); } // 判斷字符ASCII值是否A-Z之間 else if (intNumber[j] >= 65 && intNumber[j] <= 90 ) { strAsciiName += Convert.ToChar(intNumber[j]).ToString(); } // 判斷字符ASCII值是否a-z之間 else if (intNumber[j] >= 97 && intNumber[j] <= 122 ) { strAsciiName += Convert.ToChar(intNumber[j]).ToString(); } else // 判斷字符ASCII值不在以上范圍內(nèi) { if (intNumber[j] > 122 ) // 判斷字符ASCII值是否大于z { strAsciiName += Convert.ToChar(intNumber[j] - 10 ).ToString(); } else { strAsciiName += Convert.ToChar(intNumber[j] - 9 ).ToString(); } } // label3.Text = strAsciiName; // 得到注冊碼 } return strAsciiName; } else { return "" ; } } // 步驟四: 用戶輸入注冊碼注冊軟件, 演示代碼如下: // 注冊 public bool RegistIt( string currentCode, string realCode) { if (realCode != "" ) { if (currentCode.TrimEnd().Equals(realCode.TrimEnd())) { Microsoft.Win32.RegistryKey retkey = Microsoft.Win32.Registry.CurrentUser. OpenSubKey( " software " , true ).CreateSubKey( " StupidsCat " ). CreateSubKey( " StupidsCat.ini " ). CreateSubKey(currentCode.TrimEnd()); retkey.SetValue( " StupidsCat " , " BBC6D58D0953F027760A046D58D52786 " ); retkey = Microsoft.Win32.Registry.LocalMachine. OpenSubKey( " software " , true ).CreateSubKey( " StupidsCat " ). CreateSubKey( " StupidsCat.ini " ). CreateSubKey(currentCode.TrimEnd()); retkey.SetValue( " StupidsCat " , " BBC6D58D0953F027760A046D58D52786 " ); return Stupids; } else { return Cat; } } else { return Cat; } } public bool BoolRegist( string sn) { string [] keynames; bool flag = false ; Microsoft.Win32.RegistryKey localRegKey = Microsoft.Win32.Registry.LocalMachine; Microsoft.Win32.RegistryKey userRegKey = Microsoft.Win32.Registry.CurrentUser; try { keynames = localRegKey.OpenSubKey( " software\\StupidsCat\\StupidsCat.ini\\ " + GetMd5(sn)).GetValueNames(); foreach ( string name in keynames) { if (name == " StupidsCat " ) { if (localRegKey.OpenSubKey( " software\\StupidsCat\\StupidsCat.ini\\ " + GetMd5(sn)).GetValue( " StupidsCat " ).ToString() == " BBC6D58D0953F027760A046D58D52786 " ) flag = true ; } } keynames = userRegKey.OpenSubKey( " software\\StupidsCat\\StupidsCat.ini\\ " + GetMd5(sn)).GetValueNames(); foreach ( string name in keynames) { if (name == " StupidsCat " ) { if (flag && userRegKey.OpenSubKey( " software\\StupidsCat\\StupidsCat.ini\\ " + GetMd5(sn)).GetValue( " StupidsCat " ).ToString() == " BBC6D58D0953F027760A046D58D52786 " ) return true ; } } return false ; } catch { return false ; } finally { localRegKey.Close(); userRegKey.Close(); } } public string GetMd5( object text) { string path = text.ToString(); MD5CryptoServiceProvider MD5Pro = new MD5CryptoServiceProvider(); Byte[] buffer = Encoding.GetEncoding( " utf-8 " ).GetBytes(text.ToString()); Byte[] byteResult = MD5Pro.ComputeHash(buffer); string md5result = BitConverter.ToString(byteResult).Replace( " - " , "" ); return md5result; } } }
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
