首页 > 编程语言 >C#实现软件授权,限定MAC运行(软件license管理,简单软件注册机制)

C#实现软件授权,限定MAC运行(软件license管理,简单软件注册机制)

时间:2024-08-28 11:15:52浏览次数:7  
标签:info return string license C# private new 软件 public

C#实现软件授权,限定MAC运行(软件license管理,简单软件注册机制)

 

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

技术上主要三个模块:1.获取电脑相关硬件信息(可参考);2.加密解密;3.读写文件;

1.获取电脑相关硬件信息代码:

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.加密解密代码;

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.读写文件

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.其他界面代码:

主界面代码:

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("注册成功~");
        }
    }
}

 注册机代码:

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介绍),这样就不能反编译获得源码。

至此全部完成,只是个人的一些实践,对自己是一个记录,同时希望也能对别人有些帮助,如果有什么错误,还望不吝指出,共同进步,转载请保留原文地址

https://download.csdn.net/download/pfe_nova/7943235

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

标签:info,return,string,license,C#,private,new,软件,public
From: https://www.cnblogs.com/HarryK4952/p/18384225

相关文章

  • 洛谷 P3128 [USACO15DEC] Max Flow P
    洛谷P3128[USACO15DEC]MaxFlowP题意给定一棵\(n\)个点的树,给定\(k\)个点对\((u,v)\),把\(u\)到\(v\)路径上所有点的点权加一,最后求最大点权。思路树上差分模版。维护\(a_i\)表示每个点到根的加法标记。对于每个点对\((u,v)\),把\(a_u\),\(a_v\)加一,\(a_{LCA......
  • C# 中 Task
    1. Task,它代表了一个异步操作的执行和完成。可以用来封装一个异步操作,使其可以在不阻塞主线程的情况下执行,并在操作完成后获取结果。2.示例:UseMethodAsync()通过Task.Run将该操作提交给线程池,并使用await等待其完成获取到结果。staticasyncTask<int>LongOperationAsync......
  • Asp.Net Core中Typed HttpClient高级用法
    另一个常见的需求是根据不同的服务接口创建不同的HttpClient实例。为了实现这一点,ASP.NETCore提供了TypedHttpClient的支持。下面是使用TypedHttpClient的示例代码:publicinterfaceIExampleService{Task<string>GetData();}publicclassExampleService:IExampl......
  • 3D 建模和设计软件 Autodesk 3ds Max、Blender 和 AutoCAD 优缺点比较
    Autodesk3dsMax、Blender和AutoCAD是三款广泛使用的3D建模和设计软件,它们各有优缺点。以下是对这三款软件的比较:Autodesk3dsMax优点:强大的建模和渲染功能:提供丰富的建模工具和功能,特别适合建筑可视化、动画和游戏开发。强大的渲染引擎(如V-Ray、Arnold)支持高质量......
  • VMware Cloud Foundation 9 发布 - 领先的多云平台
    VMwareCloudFoundation9发布-领先的多云平台高效管理虚拟机(VM)和容器工作负载,为本地部署的全栈超融合基础架构(HCI)提供云的优势。请访问原文链接:https://sysin.org/blog/vmware-cloud-foundation-9/,查看最新版。原创作品,转载请保留出处。作者主页:sysin.orgVMware......
  • Cloud Studio:颠覆传统的云端开发与学习解决方案
    CloudStudioCloudStudio(云端IDE)是一款基于浏览器的集成开发环境,它为开发者提供了一个高效、稳定的云端工作站。用户在使用CloudStudio时,无需进行任何本地安装,只需通过浏览器即可随时随地轻松访问和使用。这种无缝的访问方式不仅提升了工作效率,也极大地简化了开发流程,使得开......
  • ABC F(500)
    ABCF(*500)ABC364F-RangeConnectMSTProblemStatementThereisagraphwith\(N+Q\)vertices,numbered\(1,2,\ldots,N+Q\).Initially,thegraphhasnoedges.Forthisgraph,performthefollowingoperationfor\(i=1,2,\ldots,Q\)......
  • RapidCMS 几个常见漏洞
    侵权声明本文章中的所有内容(包括但不限于文字、图像和其他媒体)仅供教育和参考目的。如果在本文章中使用了任何受版权保护的材料,我们满怀敬意地承认该内容的版权归原作者所有。如果您是版权持有人,并且认为您的作品被侵犯,请通过以下方式与我们联系:[[email protected]]。我们将在确......
  • CAS5和CAS6自定义异常提示消息
    CAS5和CAS6自定义异常提示消息使用cas登录时,如果登录错误页面应该提示一下错误消息,cas自带的有一些,不适用的话就需要自定义自己的异常消息提示了。自定义异常提示消息自定义异常消息类例如:验证码异常消息类importjavax.security.auth.login.AccountExpiredException;......
  • H3C-IMC智能管理中心RCE漏洞复现
    0x01漏洞描述:autoDeploy接口中存在远程代码执行漏洞,未经身份攻击者可通过该漏洞在服务器端任意执行代码,写入后门,获取服务器权限,进而控制整个web服务器。该漏洞利用难度较低,建议受影响的用户尽快修复。0x02搜索语句:Fofa:(title="用户自助服务"&&body="/selfservice/java......