(转)C#软件注册与注册机

转自:https://www.cnblogs.com/hanzhaoxin/archive/2013/01/04/2844191.html

例子下载地址:https://download.csdn.net/download/cgs_______/11862095

注册机类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;

namespace ExportLshTool
{
    class SoftReg
    {
        /// <summary>
        /// 获取硬盘卷标号
        /// </summary>
        /// <returns></returns>
        public 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 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 string GetMNum()
        {
            string strNum = GetCpu() + GetDiskVolumeSerialNumber();
            return strNum.Substring(0, 24);
        }

        public int[] intCode = new int[127];//存储密匙
        public char[] charCode = new char[25];//存储ASCII码
        public int[] intNumber = new int[25];//存储ASCII码值

        /// <summary>
        /// 初始化密匙
        /// </summary>
        public void SetIntCode()
        {
            for (int i = 1; i < intCode.Length; i++)
            {
                intCode[i] = i % 9;
            }
        }

        /// <summary>
        /// 生成注册码
        /// </summary>
        /// <returns></returns>
        public 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 i = 1; i < intNumber.Length; i++)//改变ASCII码值
            {
                intNumber[i] = Convert.ToInt32(charCode[i]) + intCode[Convert.ToInt32(charCode[i])];
            }
            string strAsciiName = "";
            for (int i = 1; i < intNumber.Length; i++)
            {
                if ((intNumber[i] >= 48 && intNumber[i] <= 57) || 
                    (intNumber[i] >= 65 && intNumber[i] <= 90) || 
                    (intNumber[i] >= 97 && intNumber[i] <= 122))
                {
                    strAsciiName += Convert.ToChar(intNumber[i]).ToString();
                }
                else if (intNumber[i] > 122) //判断如果大于Z
                {
                    strAsciiName += Convert.ToChar(intNumber[i] - 10).ToString();
                }
                else
                {
                    strAsciiName += Convert.ToChar(intNumber[i] - 9).ToString();
                }
            }
            return strAsciiName;
        }

        /// <summary>
        /// 生成注册码
        /// </summary>
        /// <returns></returns>
        public string GetRNum(string strMNum)
        {
            SetIntCode();

            for (int i = 1; i < charCode.Length; i++)//存储机器码
            {
                charCode[i] = Convert.ToChar(strMNum.Substring(i - 1, 1));
            }
            for (int i = 1; i < intNumber.Length; i++)//改变ASCII码值
            {
                intNumber[i] = Convert.ToInt32(charCode[i]) + intCode[Convert.ToInt32(charCode[i])];
            }
            string strAsciiName = "";
            for (int i = 1; i < intNumber.Length; i++)
            {
                if ((intNumber[i] >= 48 && intNumber[i] <= 57) ||
                    (intNumber[i] >= 65 && intNumber[i] <= 90) ||
                    (intNumber[i] >= 97 && intNumber[i] <= 122))
                {
                    strAsciiName += Convert.ToChar(intNumber[i]).ToString();
                }
                else if (intNumber[i] > 122) //判断如果大于Z
                {
                    strAsciiName += Convert.ToChar(intNumber[i] - 10).ToString();
                }
                else
                {
                    strAsciiName += Convert.ToChar(intNumber[i] - 9).ToString();
                }
            }
            return strAsciiName;
        }

    }
}

写入注册表操作(需要using Microsoft.Win32)

RegistryKey retKey = Registry.CurrentUser.OpenSubKey("SoftWare", true).CreateSubKey("mySoftWare").CreateSubKey("Register.INI").CreateSubKey(txt_rnum.Text);
                    retKey.SetValue("UserName", "Rsoft");

读注册表操作(需要using Microsoft.Win32)

RegistryKey retKey = Registry.CurrentUser.OpenSubKey("SoftWare", true).CreateSubKey("mySoftWare").CreateSubKey("Register.INI");
            foreach (string strRNum in retKey.GetSubKeyNames())
            {
                if (strRNum == softReg.GetRNum())
                {
                   //逻辑
                }
            }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值