首页 > 代码库 > .Net常用技巧_软件注册码

.Net常用技巧_软件注册码

using System;using System.Collections.Generic;using System.Text;using System.Management;using Utility;using EXDataControl;using System.Collections;using Microsoft.Win32;namespace MyTool{    public class SoftReg    {        /// <summary>        /// 获取硬盘卷标号        /// </summary>        /// <returns></returns>        public static string GetDiskVolumeSerialNumber()        {            ManagementClass mc = new ManagementClass("win32_NetworkAdapterConfiguration");            ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");            disk.Get();            return disk.GetPropertyValue("VolumeSerialNumber").ToString();        }        /// <summary>        /// 获取CPU序列号        /// </summary>        /// <returns></returns>        public static string GetCpu()        {            string strCpu = null;            ManagementClass myCpu = new ManagementClass("win32_Processor");            ManagementObjectCollection myCpuCollection = myCpu.GetInstances();            foreach (ManagementObject myObject in myCpuCollection)            {                strCpu = myObject.Properties["Processorid"].Value.ToString();            }            return strCpu;        }        /// <summary>        /// 生成机器码        /// </summary>        /// <returns></returns>        public static string GetMNum()        {            string strNum = GetCpu() + GetDiskVolumeSerialNumber();            string strMNum = strNum.Substring(0, 24);    //截取前24位作为机器码            return strMNum;        }        public static int[] intCode = new int[127];    //存储密钥        public static char[] charCode = new char[25];  //存储ASCII码        public static int[] intNumber = new int[25];   //存储ASCII码值        //初始化密钥        public static void SetIntCode()        {            for (int i = 1; i < intCode.Length; i++)            {                intCode[i] = i % 9;            }        }        /// <summary>        /// 生成注册码        /// </summary>        /// <returns></returns>        public static string GetRNum()        {            SetIntCode();            string strMNum = GetMNum();            for (int i = 1; i < charCode.Length; i++)   //存储机器码            {                charCode[i] = Convert.ToChar(strMNum.Substring(i - 1, 1));            }            for (int j = 1; j < intNumber.Length; j++)  //改变ASCII码值            {                intNumber[j] = Convert.ToInt32(charCode[j]) + intCode[Convert.ToInt32(charCode[j])];            }            string strAsciiName = "";   //注册码            for (int k = 1; k < intNumber.Length; k++)  //生成注册码            {                if ((intNumber[k] >= 48 && intNumber[k] <= 57) || (intNumber[k] >= 65 && intNumber[k]                    <= 90) || (intNumber[k] >= 97 && intNumber[k] <= 122))  //判断如果在0-9、A-Z、a-z之间                {                    strAsciiName += Convert.ToChar(intNumber[k]).ToString();                }                else if (intNumber[k] > 122)  //判断如果大于z                {                    strAsciiName += Convert.ToChar(intNumber[k] - 10).ToString();                }                else                {                    strAsciiName += Convert.ToChar(intNumber[k] - 9).ToString();                }            }            string strEncrypt = Utility.DESEncrypt.Encrypt(strAsciiName);     //加密            return strEncrypt;        }        /// <summary>        /// 生成试用版注册码(取注册码,前N位改成当前日期)        /// </summary>        /// <returns></returns>        public static string GetBNum(EXDataControl.EXDataCenter dc)        {            string strDecrypt = Utility.DESEncrypt.Decrypt(GetRNum());  //先解密注册码            string CurrentDateTime = dc.getServerDateTime().Substring(0, 10);            strDecrypt = strDecrypt.Substring(10, strDecrypt.Length - 10);            string strKey = CurrentDateTime + strDecrypt;   //10位日期+注册码后14位            string strEncrypt = Utility.DESEncrypt.Encrypt(strKey);     //加密            return strEncrypt;        }    }}