1 using System; 2 using System.Management; 3 4 namespace SoftReg 5 { 6 internal class SoftReg 7 { 8 #region 变量 9 public int[] IntCode = new int[127]; //存储密钥 10 public char[] CharCode = new char[25]; //存储ASCII码 11 public int[] IntNumber = new int[25]; //存储ASCII码值 12 #endregion 13 14 #region 方法 15 /// <summary> 16 /// 初始化存储密钥 17 /// </summary> 18 public void SetIntCode() 19 { 20 for (int i = 1; i < IntCode.Length; i++) 21 { 22 IntCode[i] = i % 9; 23 } 24 } 25 26 ///<summary> 27 /// 获取硬盘卷标号 28 ///</summary> 29 public string GetDiskVolumeSerialNumber() 30 { 31 //ManagementClass mc = new ManagementClass("win32_NetworkAdapterConfiguration"); 32 ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\""); 33 disk.Get(); 34 return disk.GetPropertyValue("VolumeSerialNumber").ToString(); 35 } 36 37 ///<summary> 38 /// 获取CPU序列号 39 ///</summary> 40 public string GetCpu() 41 { 42 string strCpu = null; 43 ManagementClass myCpu = new ManagementClass("win32_Processor"); 44 ManagementObjectCollection myCpuCollection = myCpu.GetInstances(); 45 foreach (ManagementObject myObject in myCpuCollection) 46 { 47 strCpu = myObject.Properties["Processorid"].Value.ToString(); 48 } 49 return strCpu; 50 } 51 52 ///<summary> 53 /// 生成机器码(机器码由CPU序列号+硬盘卷标号合成)----可扩展 54 ///</summary> 55 public string GetMNum() 56 { 57 string strNum = GetCpu() + GetDiskVolumeSerialNumber(); 58 string strMNum = strNum.Substring(0, 24); //截取前24位作为机器码 59 return strMNum; 60 } 61 62 ///<summary> 63 /// 生成注册码(根据本机机器码生成注册码) 64 ///</summary> 65 public string GetRNum() 66 { 67 SetIntCode(); 68 string strMNum = GetMNum(); 69 for (int i = 1; i < CharCode.Length; i++) //存储机器码 70 { 71 CharCode[i] = Convert.ToChar(strMNum.Substring(i - 1, 1)); 72 } 73 for (int j = 1; j < IntNumber.Length; j++) //改变ASCII码值 74 { 75 IntNumber[j] = Convert.ToInt32(CharCode[j]) + IntCode[Convert.ToInt32(CharCode[j])]; 76 } 77 string strAsciiName = ""; //注册码 78 for (int k = 1; k < IntNumber.Length; k++) //生成注册码 79 { 80 81 if ((IntNumber[k] >= 48 && IntNumber[k] <= 57) || (IntNumber[k] >= 65 && IntNumber[k] 82 <= 90) || (IntNumber[k] >= 97 && IntNumber[k] <= 122)) 83 //判断如果在0-9、A-Z、a-z之间 84 { 85 strAsciiName += Convert.ToChar(IntNumber[k]).ToString(); 86 } 87 else if (IntNumber[k] > 122) //判断如果大于z 88 { 89 strAsciiName += Convert.ToChar(IntNumber[k] - 10).ToString(); 90 } 91 else 92 { 93 strAsciiName += Convert.ToChar(IntNumber[k] - 9).ToString(); 94 } 95 } 96 return strAsciiName; 97 } 98 99 ///<summary> 100 /// 生成注册码(根据传入的机器码生成注册码) 101 ///</summary> 102 ///<returns>机器码</returns> 103 public string GetRNum(string machineStr) 104 { 105 SetIntCode(); 106 string strMNum = machineStr; 107 for (int i = 1; i < CharCode.Length; i++) //存储机器码 108 { 109 CharCode[i] = Convert.ToChar(strMNum.Substring(i - 1, 1)); 110 } 111 for (int j = 1; j < IntNumber.Length; j++) //改变ASCII码值 112 { 113 IntNumber[j] = Convert.ToInt32(CharCode[j]) + IntCode[Convert.ToInt32(CharCode[j])]; 114 } 115 string strAsciiName = ""; //注册码 116 for (int k = 1; k < IntNumber.Length; k++) //生成注册码 117 { 118 119 if ((IntNumber[k] >= 48 && IntNumber[k] <= 57) || (IntNumber[k] >= 65 && IntNumber[k] 120 <= 90) || (IntNumber[k] >= 97 && IntNumber[k] <= 122)) 121 //判断如果在0-9、A-Z、a-z之间 122 { 123 strAsciiName += Convert.ToChar(IntNumber[k]).ToString(); 124 } 125 else if (IntNumber[k] > 122) //判断如果大于z 126 { 127 strAsciiName += Convert.ToChar(IntNumber[k] - 10).ToString(); 128 } 129 else 130 { 131 strAsciiName += Convert.ToChar(IntNumber[k] - 9).ToString(); 132 } 133 } 134 return strAsciiName; 135 } 136 #endregion 137 } 138 }