软件License授权原理

在数字时代,软件已成为我们日常生活和工作中不可或缺的一部分。为了保护软件的知识产权,并确保其合法使用,软件授权机制应运而生。本文将深入探讨软件License授权的原理及其重要性。

二、软件License授权的原理

  1. 许可证密钥

我们做的商业软件需要进行售卖,为了收取费用,一般需要一个软件使用许可证,然后输入这个许可到软件里就能够使用软件(比如,一串序列码或者一个许可证文件)。于是有的小伙伴就开始好奇这个许可是怎么实现的。

实现许可证的关键点1. 如何控制只在指定设备上使用

如果不控制指定设备,那么下发了许可证,只要把软件复制多份安装则可到处使用,不利于版权维护。但我们想想,有什么办法唯一标识一台电脑?答案就是:mac地址,ip地址,主板序列号等。在许可证中指定这些唯一标识即可实现指定设备使用。

实现许可证的关键点2. 如何控制软件使用期限

为了版权可持续性收益,对软件使用设置期限,到期续费等,则需要在许可证中配置使用起止日期。

  1. 在线验证

    • 越来越多的软件采用在线验证机制,即软件在运行时会定期或不定期地连接到开发者的服务器进行验证。这时就可以验证软件的使用期限了。

    • 如果验证失败,软件可能会限制某些功能或完全停止工作。

  2. 数字签名

    • 数字签名技术用于验证软件的完整性和来源。通过对软件进行哈希处理并使用开发者的私钥进行加密,可以生成一个数字签名。

    • 当用户安装或运行软件时,系统会使用开发者的公钥来验证签名。如果签名有效,则说明软件是原始的、未被篡改的。

三、软件License授权的重要性

  1. 知识产权保护:软件License授权是保护知识产权的重要手段。通过限制非法复制和分发,它确保了软件开发者的创意和劳动成果得到应有的回报。

  2. 维护市场秩序:合法的软件授权有助于维护一个公平的市场环境,防止不正当竞争和侵权行为。

  3. 用户权益保障:正版软件通常提供更好的技术支持和更新服务,确保用户能够享受到高质量的产品体验。

  4. 促进软件创新:当软件开发者的权益得到充分保护时,他们更有动力投入研发,推出更多创新的产品和功能。

     

    四.核心源码

    由于篇幅限制,下面只列出一些关键的C#源码信息,其它部分请开发者自行思考+补足。

  • 电脑信息获取

  1. 主要通过ManagementClass进行获取客户端电脑硬件相关配置信息,如下所示:

  2. using Microsoft.Win32;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Management;
    using System.Net.NetworkInformation;
    using System.Text;
    using System.Threading.Tasks;
     
    namespace DemoLicence.Common
    {
        public class ComputerHelper
        {
            public static Dictionary<string,string> GetComputerInfo()
            {
                var info = new Dictionary<string,string>();
                string cpu = GetCPUInfo();
                string baseBoard = GetBaseBoardInfo();
                string bios = GetBIOSInfo();
                string mac = GetMACInfo();
                info.Add("cpu", cpu);
                info.Add("baseBoard", baseBoard);
                info.Add("bios", bios);
                info.Add("mac", 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 = GetMacAddress();//GetHardWareInfo("Win32_NetworkAdapterConfiguration", "MACAddress");
                return info;
            }
     
            private static string GetMacAddress()
            {
                var mac = "";
                var mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                var moc = mc.GetInstances();
                foreach (var o in moc)
                {
                    var mo = (ManagementObject)o;
                    if (!(bool)mo["IPEnabled"]) continue;
                    mac = mo["MacAddress"].ToString();
                    break;
                }
                return mac;
            }
     
            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;
            }
        }
    }

  • RSA非对称加密

  1. 主要对客户端提供的电脑信息及有效期等内容,进行非对称加密,如下所示:

    public class RSAHelper
    {
     
      private static string keyContainerName = "star";
     
      private static string m_PriKey = string.Empty;
     
      private static string m_PubKey = string.Empty;
     
     
      public static string PriKey
      {
        get
        {
          return m_PriKey;
        }
     
        set
        {
          m_PriKey = value;
        }
      }
     
      public static string PubKey
      {
        get
        {
          return m_PubKey;
        }
     
        set
        {
          m_PubKey = value;
        }
      }
     
      public static string Encrypto(string source)
      {
        if (string.IsNullOrEmpty(m_PubKey) && string.IsNullOrEmpty(m_PriKey))
        {
          generateKey();
        }
        return getEncryptoInfoByRSA(source);
      }
     
      public static string Decrypto(string dest)
      {
        if (string.IsNullOrEmpty(m_PubKey) && string.IsNullOrEmpty(m_PriKey))
        {
          generateKey();
        }
        return getDecryptoInfoByRSA(dest);
      }
     
      public static void generateKey()
      {
        CspParameters m_CspParameters;
        m_CspParameters = new CspParameters();
        m_CspParameters.KeyContainerName = keyContainerName;
        RSACryptoServiceProvider asym = new RSACryptoServiceProvider(m_CspParameters);
        m_PriKey = asym.ToXmlString(true);
        m_PubKey = asym.ToXmlString(false);
        asym.PersistKeyInCsp = false;
        asym.Clear();
      }
     
      private static string getEncryptoInfoByRSA(string source)
      {
        byte[] plainByte = Encoding.ASCII.GetBytes(source);
        //初始化参数
        RSACryptoServiceProvider asym = new RSACryptoServiceProvider();
        asym.FromXmlString(m_PubKey);
        int keySize = asym.KeySize / 8;//非对称加密,每次的长度不能太长,否则会报异常
        int bufferSize = keySize - 11;
        if (plainByte.Length > bufferSize)
        {
          throw new Exception("非对称加密最多支持【" + bufferSize + "】字节,实际长度【" + plainByte.Length + "】字节。");
        }
        byte[] cryptoByte = asym.Encrypt(plainByte, false);
        return Convert.ToBase64String(cryptoByte);
      }
     
      private static string getDecryptoInfoByRSA(string dest)
      {
        byte[] btDest = Convert.FromBase64String(dest);
        //初始化参数
        RSACryptoServiceProvider asym = new RSACryptoServiceProvider();
        asym.FromXmlString(m_PriKey);
        int keySize = asym.KeySize / 8;//非对称加密,每次的长度不能太长,否则会报异常
                         //int bufferSize = keySize - 11;
        if (btDest.Length > keySize)
        {
          throw new Exception("非对称解密最多支持【" + keySize + "】字节,实际长度【" + btDest.Length + "】字节。");
        }
        byte[] cryptoByte = asym.Decrypt(btDest, false);
        return Encoding.ASCII.GetString(cryptoByte);
      }
    }

五、结论

软件License授权不仅是保护知识产权的重要工具,也是维护市场秩序和促进软件行业健康发展的关键因素。随着技术的进步和法律的完善,我们有理由相信,未来的软件授权机制将更加智能、高效和安全,为用户和开发者带来更好的体验。

smart-license是一款用于安全加固的开源项目。主要服务于非开源产品、商业软件、具备试用功能的付费软件等,为软件提供授权制的使用方式。smart-license适用场景非开源产品、商业软件、收费软件。 限制产品的传播性,每个客户拥有专属 License。 同一款软件发行包根据 License 的不同提供不同的服务能力。 限定软件授权时效smart-license特色开源,代码完全公开,License的生成原理是透明的。 易用,提供二进制包,直接基于命令行生成 License。 安全,生成的 License 在一定程度上具备防篡改能力,破解难度大。 安全加固,采用非对称加密方式对 License源数据 进行预处理,防止伪造License。smart-license使用方式生成License 1、下载smart-license.tar.gz包,解压 2、进入bin目录执行以下命令,例如:./license.sh 1d HelloWorld。 1d:表示授权效期1天,即一天后该License便过期。支持的效期格式包括: h,1h:1小时; 2h:2小时 d,1d:1天; 10d:10天 y,1y:1年; 2y:2年 HelloWorld:表示待加密的license内容。 实际场景下可以通过license授权不同的产品功能和有效期,例如:./license.sh 1y features_1:on;features_2:off; 如果待授权license内容为文件,可以采用同样的命令,例如:./license.sh 1y config.properties 3、执行成功后,会在当前目录下生成 License:license.txt以及 License源文件:source.txt。 注意:license.txt是提供给客户的授权文件;而source.txt是由软件提供方持有,其中包含加密私钥,需要妥善保使用License 1、引入Maven依赖 org.smartboot.license license-client 1.0.0-SNAPSHOT 2、载入License。如若License已过期,则会触发异常。 public class LicenseTest { public static void main(String[] args) throws Exception { File file=new File("license.txt"); License license = new License(); LicenseConfig licenseConfig=license.loadLicense(file); System.out.println(licenseData.getOriginal()); } } 3、获取licenseData并以此配置启动软件。smart-license截图
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值