C#软件授权、注册、加密、解密模块源码解析并制作注册机生成license

最近做了一个绿色免安装软件,领导临时要求加个注册机制,不能让现场工程师随意复制。事出突然,只能在现场开发(离开现场软件就不受我们控了)。花了不到两个小时实现了简单的注册机制,稍作整理。
        基本原理:1.软件一运行就把计算机的CPU、主板、BIOS、MAC地址记录下来,然后加密(key=key1)生成文件;2.注册机将该文件内容MD5加密后再进行一次加密(key=key2)保存成注册文件;3.注册验证的逻辑,计算机信息加密后(key=key1)加密md5==注册文件解密(key=key2);
另外,采用ConfuserEx将可执行文件加密;这样别人要破解也就需要点力气了(没打算防破解,本意只想防复制的),有能力破解的人也不在乎破解这个软件了(开发这个软件前后只花了一周时间而已);
        技术上主要三个模块:1.获取电脑相关硬件信息(可参考);2.加密解密;3.读写文件;

        1.获取电脑相关硬件信息代码:
[csharp] view plain copy
public class ComputerInfo  
{  
    public static string GetComputerInfo()  
    {  
        string info = string.Empty;  
        string cpu = GetCPUInfo();  
        string baseBoard = GetBaseBoardInfo();  
        string bios = GetBIOSInfo();  
        string mac = GetMACInfo();  
        info = string.Concat(cpu, baseBoard, bios, mac);  
        return info;  
    }  
  
    private static string GetCPUInfo()  
    {  
        string info = string.Empty;  
        info = GetHardWareInfo("Win32_Processor", "ProcessorId");  
        return info;  
    }  
    private static string GetBIOSInfo()  
    {  
        string info = string.Empty;  
        info = GetHardWareInfo("Win32_BIOS", "SerialNumber");  
        return info;  
    }  
    private static string GetBaseBoardInfo()  
    {  
        string info = string.Empty;  
        info = GetHardWareInfo("Win32_BaseBoard", "SerialNumber");  
        return info;  
    }  
    private static string GetMACInfo()  
    {  
        string info = string.Empty;  
        info = GetHardWareInfo("Win32_BaseBoard", "SerialNumber");  
        return info;  
    }  
    private static string GetHardWareInfo(string typePath, string key)  
    {  
        try  
        {  
            ManagementClass managementClass = new ManagementClass(typePath);  
            ManagementObjectCollection mn = managementClass.GetInstances();  
            PropertyDataCollection properties = managementClass.Properties;  
            foreach (PropertyData property in properties)  
            {  
                if (property.Name == key)  
                {  
                    foreach (ManagementObject m in mn)  
                    {  
                        return m.Properties[property.Name].Value.ToString();  
                    }  
                }  
  
            }  
        }  
        catch (Exception ex)  
        {  
            //这里写异常的处理    
        }  
        return string.Empty;  
    }  
    private static string GetMacAddressByNetworkInformation()  
    {  
        string key = "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\";  
        string macAddress = string.Empty;  
        try  
        {  
            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();  
            foreach (NetworkInterface adapter in nics)  
            {  
                if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet  
                    && adapter.GetPhysicalAddress().ToString().Length != 0)  
                {  
                    string fRegistryKey = key + adapter.Id + "\\Connection";  
                    RegistryKey rk = Registry.LocalMachine.OpenSubKey(fRegistryKey, false);  
                    if (rk != null)  
                    {  
                        string fPnpInstanceID = rk.GetValue("PnpInstanceID", "").ToString();  
                        int fMediaSubType = Convert.ToInt32(rk.GetValue("MediaSubType", 0));  
                        if (fPnpInstanceID.Length > 3 &&  
                            fPnpInstanceID.Substring(0, 3) == "PCI")  
                        {  
                            macAddress = adapter.GetPhysicalAddress().ToString();  
                            for (int i = 1; i < 6; i++)  
                            {  
                                macAddress = macAddress.Insert(3 * i - 1, ":");  
                            }  
                            break;  
                        }  
                    }  
  
                }  
            }  
        }  
        catch (Exception ex)  
        {  
            //这里写异常的处理    
        }  
        return macAddress;  
    }  
}  
        2.加密解密代码;
[csharp] view plain copy
public enum EncryptionKeyEnum  
{  
    KeyA,  
    KeyB  
}  
public class EncryptionHelper  
{  
    string encryptionKeyA = "pfe_Nova";  
    string encryptionKeyB = "WorkHard";  
    string md5Begin = "Hello";  
    string md5End = "World";  
    string encryptionKey = string.Empty;  
    public EncryptionHelper()  
    {  
        this.InitKey();  
    }  
    public EncryptionHelper(EncryptionKeyEnum key)  
    {  
        this.InitKey(key);  
    }  
    private void InitKey(EncryptionKeyEnum key = EncryptionKeyEnum.KeyA)  
    {  
        switch (key)  
        {  
            case EncryptionKeyEnum.KeyA:  
                encryptionKey = encryptionKeyA;  
                break;  
            case EncryptionKeyEnum.KeyB:  
                encryptionKey = encryptionKeyB;  
                break;  
        }  
    }  
  
    public string EncryptString(string str)  
    {  
        return Encrypt(str, encryptionKey);  
    }  
    public string DecryptString(string str)  
    {  
        return Decrypt(str, encryptionKey);  
    }  
    public string GetMD5String(string str)  
    {  
        str = string.Concat(md5Begin, str, md5End);  
        MD5 md5 = new MD5CryptoServiceProvider();  
        byte[] fromData = Encoding.Unicode.GetBytes(str);  
        byte[] targetData = md5.ComputeHash(fromData);  
        string md5String = string.Empty;  
        foreach (var b in targetData)  
            md5String += b.ToString("x2");  
        return md5String;  
    }  
  
    private string Encrypt(string str, string sKey)  
    {  
        DESCryptoServiceProvider des = new DESCryptoServiceProvider();  
        byte[] inputByteArray = Encoding.Default.GetBytes(str);  
        des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);  
        des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);  
        MemoryStream ms = new MemoryStream();  
        CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);  
        cs.Write(inputByteArray, 0, inputByteArray.Length);  
        cs.FlushFinalBlock();  
        StringBuilder ret = new StringBuilder();  
        foreach (byte b in ms.ToArray())  
        {  
            ret.AppendFormat("{0:X2}", b);  
        }  
        ret.ToString();  
        return ret.ToString();  
    }  
    private string Decrypt(string pToDecrypt, string sKey)  
    {  
        DESCryptoServiceProvider des = new DESCryptoServiceProvider();  
        byte[] inputByteArray = new byte[pToDecrypt.Length / 2];  
        for (int x = 0; x < pToDecrypt.Length / 2; x++)  
        {  
            int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));  
            inputByteArray[x] = (byte)i;  
        }  
        des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);  
        des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);  
        MemoryStream ms = new MemoryStream();  
        CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);  
        cs.Write(inputByteArray, 0, inputByteArray.Length);  
        cs.FlushFinalBlock();  
        StringBuilder ret = new StringBuilder();  
        return System.Text.Encoding.Default.GetString(ms.ToArray());  
    }  
}  
        注:这边在MD5时前后各加了一段字符,这样增加一点破解难度。
        3.读写文件
[csharp] view plain copy
public class RegistFileHelper  
{  
    public static string ComputerInfofile = "ComputerInfo.key";  
    public static string RegistInfofile = "RegistInfo.key";  
    public static void WriteRegistFile(string info)  
    {  
        WriteFile(info, RegistInfofile);  
    }  
    public static void WriteComputerInfoFile(string info)  
    {  
        WriteFile(info, ComputerInfofile);  
    }  
    public static string ReadRegistFile()  
    {  
        return ReadFile(RegistInfofile);  
    }  
    public static string ReadComputerInfoFile()  
    {  
        return ReadFile(ComputerInfofile);  
    }  
    public static bool ExistComputerInfofile()  
    {  
        return File.Exists(ComputerInfofile);  
    }  
    public static bool ExistRegistInfofile()  
    {  
        return File.Exists(RegistInfofile);  
    }  
    private static void WriteFile(string info, string fileName)  
    {  
        try  
        {  
            using (StreamWriter sw = new StreamWriter(fileName, false))  
            {  
                sw.Write(info);  
                sw.Close();  
            }  
        }  
        catch (Exception ex)  
        {  
        }  
    }  
    private static string ReadFile(string fileName)  
    {  
        string info = string.Empty;  
        try  
        {  
            using (StreamReader sr = new StreamReader(fileName))  
            {  
                info = sr.ReadToEnd();  
                sr.Close();  
            }  
        }  
        catch (Exception ex)  
        {  
        }  
        return info;  
    }  
}  
        4.其他界面代码:
        主界面代码:
[csharp] view plain copy
public partial class FormMain : Form  
{  
    private string encryptComputer = string.Empty;  
    private bool isRegist = false;  
    private const int timeCount = 30;  
    public FormMain()  
    {  
        InitializeComponent();  
        Control.CheckForIllegalCrossThreadCalls = false;  
    }  
    private void FormMain_Load(object sender, EventArgs e)  
    {  
        string computer = ComputerInfo.GetComputerInfo();  
        encryptComputer = new EncryptionHelper().EncryptString(computer);  
        if (CheckRegist() == true)  
        {  
            lbRegistInfo.Text = "已注册";  
        }  
        else  
        {  
            lbRegistInfo.Text = "待注册,运行十分钟后自动关闭";  
            RegistFileHelper.WriteComputerInfoFile(encryptComputer);  
            TryRunForm();  
        }  
    }  
    /// <summary>  
    /// 试运行窗口  
    /// </summary>  
    private void TryRunForm()  
    {  
        Thread threadClose = new Thread(CloseForm);  
        threadClose.IsBackground = true;  
        threadClose.Start();  
    }  
    private bool CheckRegist()  
    {  
        EncryptionHelper helper = new EncryptionHelper();  
        string md5key = helper.GetMD5String(encryptComputer);  
        return CheckRegistData(md5key);  
    }  
    private bool CheckRegistData(string key)  
    {  
        if (RegistFileHelper.ExistRegistInfofile() == false)  
        {  
            isRegist = false;  
            return false;  
        }  
        else  
        {  
            string info = RegistFileHelper.ReadRegistFile();  
            var helper = new EncryptionHelper(EncryptionKeyEnum.KeyB);  
            string registData = helper.DecryptString(info);  
            if (key == registData)  
            {  
                isRegist = true;  
                return true;  
            }  
            else  
            {  
                isRegist = false;  
                return false;  
            }  
        }  
    }  
    private void CloseForm()  
    {  
        int count = 0;  
        while (count < timeCount && isRegist == false)  
        {  
            if (isRegist == true)  
            {  
                return;  
            }  
            Thread.Sleep(1 * 1000);  
            count++;  
        }  
        if (isRegist == true)  
        {  
            return;  
        }  
        else  
        {  
            this.Close();  
        }  
    }  
  
    private void btnRegist_Click(object sender, EventArgs e)  
    {  
        if (lbRegistInfo.Text == "已注册")  
        {  
            MessageBox.Show("已经注册~");  
            return;  
        }  
        string fileName = string.Empty;  
        OpenFileDialog openFileDialog = new OpenFileDialog();  
        if (openFileDialog.ShowDialog() == DialogResult.OK)  
        {  
            fileName = openFileDialog.FileName;  
        }  
        else  
        {  
            return;  
        }  
        string localFileName = string.Concat(  
            Environment.CurrentDirectory,  
            Path.DirectorySeparatorChar,  
            RegistFileHelper.RegistInfofile);  
        if (fileName != localFileName)  
            File.Copy(fileName, localFileName, true);  
  
        if (CheckRegist() == true)  
        {  
            lbRegistInfo.Text = "已注册";  
            MessageBox.Show("注册成功~");  
        }  
    }  
}  
        注册机代码:
[csharp] view plain copy
public partial class FormMain : Form  
{  
    public FormMain()  
    {  
        InitializeComponent();  
    }  
  
    private void btnRegist_Click(object sender, EventArgs e)  
    {  
        string fileName = string.Empty;  
        OpenFileDialog openFileDialog = new OpenFileDialog();  
        if (openFileDialog.ShowDialog() == DialogResult.OK)  
        {  
            fileName = openFileDialog.FileName;  
        }  
        else  
        {  
            return;  
        }  
        string localFileName = string.Concat(  
            Environment.CurrentDirectory,  
            Path.DirectorySeparatorChar,  
            RegistFileHelper.ComputerInfofile);  
  
        if (fileName != localFileName)  
            File.Copy(fileName, localFileName, true);  
        string computer = RegistFileHelper.ReadComputerInfoFile();  
        EncryptionHelper help = new EncryptionHelper(EncryptionKeyEnum.KeyB);  
        string md5String = help.GetMD5String(computer);  
        string registInfo = help.EncryptString(md5String);  
        RegistFileHelper.WriteRegistFile(registInfo);  
        MessageBox.Show("注册码已生成");  
    }  
}  
        最后采用ConfuserEx将可执行文件加密(ConfuserEx介绍),这样就不能反编译获得源码。
--------------------- 
作者:weixin_37691493 
来源:CSDN 
原文:https://blog.csdn.net/weixin_37691493/article/details/79716050 
版权声明:本文为博主原创文章,转载请附上博文链接!

### 回答1: 关于"c"的问题可能有很多种解释,以下给出几种可能的回答。 1. "c"是拉丁字母表中的第三个字母。拉丁字母表是用来写大部分西方语言的字母系统。 2. "c"是计算机领域中的一个重要概念,代表着"C语言"。C语言是一种通用的高级计算机编程语言,由Dennis Ritchie在20世纪70年代开发。它被广泛应用于软件开发、操作系统和嵌入式系统等领域。 3. 在化学中,"c"可能是指摄氏度(Celsius)的缩写。摄氏度是一个温度单位,常用于描述气温和室内温度。 4. 在数学符号中,"c"可能表示光速(speed of light)。光速是物理学中一个重要的常数,它是光在真空中传播的速度。 这些只是关于"c"的几个可能的回答,并不代表全部。具体根据问题的背景和语境可能存在其他解释。 ### 回答2: c是英文字母表中的第三个字母,它在英语中有很多不同的用途和含义。 首先,c是代表“度量衡单位”中的一部分。例如,c(Celsius)代表摄氏度,是温度计中使用的一种度量单位。此外,c(centimeter)也是长度单位,等于一米的百分之一。这些度量单位在科学、工程和日常生活中非常常见。 此外,c还可以代表一些常用的词汇。例如,c(cat)代表猫,是一种常见的宠物。c(clock)代表时钟,用于测量时间的设备。c(coffee)代表咖啡,是一种受欢迎的饮品。 另外,c还可以是代表一些专业术语中的缩写。例如,c(carbon)是指碳元素,在化学和生物学中都有重要的作用。c(cosine)是指余弦,在数学和物理中用于计算角度和三角形的属性。 总而言之,c是英文字母表中一个多功能的字母,它代表着度量衡单位、常见的词汇和专业术语中的缩写。它在不同的领域和语境中都有广泛的应用。 ### 回答3: C是一种用于编程的高级语言。它是由美国贝尔实验室的丹尼斯·里奇在20世纪70年代早期开发的。C语言在计算机科学和软件开发领域广泛应用。 C语言具有简洁、高效、可移植和可扩展的特点。它提供了丰富的语法和库函数,使得开发人员可以轻松地实现各种功能。C语言的语法和结构直观清晰,易于学习和理解,因此成为许多计算机科学专业的入门语言。 C语言在系统编程、嵌入式系统和操作系统等方面有着广泛应用。因为C语言可以直接访问内存和硬件,使得开发者可以更加灵活地控制程序的执行过程。此外,C语言还具备高效的执行速度和较低的内存占用,在资源受限的环境中表现出色。 C语言还被广泛用于编写各种软件应用和工具,如编译器、数据库、图形界面和网络通信等。许多软件公司都使用C语言作为主要的开发语言,因为它兼具高效和可移植性。 总而言之,C语言是一种功能强大且广泛应用的编程语言,它有助于开发者实现各种复杂的应用和系统。通过学习和运用C语言,人们可以更好地理解计算机底层原理,并提升编程技能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值