首页 > 其他分享 >利用RSA加密打造强大License验证,确保软件正版合法运行

利用RSA加密打造强大License验证,确保软件正版合法运行

时间:2024-02-16 09:01:06浏览次数:21  
标签:正版 string License 验证 RSA rsa 公钥 软件

 

概述:C#软件开发中,License扮演着确保软件合法使用的重要角色。采用RSA非对称加密方案,服务端生成带签名的License,客户端验证其有效性,从而实现对软件的授权与安全保障。

License应用场景:

License(许可证)在C#软件开发中被广泛应用,以确保软件在合法授权的环境中运行。常见场景包括商业软件、桌面应用、服务端应用等。

Licence实现方案:

一种常见的License实现方案是使用非对称加密技术,将License信息加密,并在软件中内置公钥,从而确保只有使用私钥签名的License才会被验证通过。

Licence验证流程图:

以下是一个简单的License验证流程图:

  +-------------------+
  | 用户获取软件并安装 |
  +-------------------+
            |
            v
  +-------------------+
  |    启动软件并输入   |
  |      License信息     |
  +-------------------+
            |
            v
  +-------------------+
  |   软件解密并验证   |
  |    License的有效性  |
  +-------------------+
            |
   +--------+---------+
   |                  |
   v                  v
 有效       License无效,显示
        提示信息或阻止软件运行

主要功能代码:

以下是一个简单的C#示例,演示了使用RSA非对称加密进行License验证的基本实现。示例中包含服务端和客户端的代码。

服务端(生成License):

using System.Security.Cryptography;
using System.Text;

public class LicenseGenerator
{
    // 生成License的方法
    public string GenerateLicense()
    {
        using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
        {
            // 生成公钥和私钥
            string publicKey = rsa.ToXmlString(false);
            string privateKey = rsa.ToXmlString(true);

            // License信息(模拟)
            string licenseInfo = "ValidLicenseInfo";

            // 使用私钥对License信息进行签名
            byte[] signature = rsa.SignData(Encoding.UTF8.GetBytes(licenseInfo), new SHA256CryptoServiceProvider());

            // 将公钥、License信息和签名组合成License
            string license = $"{publicKey};{licenseInfo};{Convert.ToBase64String(signature)}";

            return license;
        }
    }
}

客户端(验证License):

using System.Security.Cryptography;
using System.Text;

public class LicenseValidator
{
    // 验证License的方法
    public bool ValidateLicense(string userEnteredKey)
    {
        // 将License拆分成公钥、License信息和签名
        string[] parts = userEnteredKey.Split(';');
        string publicKey = parts[0];
        string licenseInfo = parts[1];
        byte[] signature = Convert.FromBase64String(parts[2]);

        using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
        {
            // 设置公钥
            rsa.FromXmlString(publicKey);

            // 使用公钥验证License信息的签名
            return rsa.VerifyData(Encoding.UTF8.GetBytes(licenseInfo), new SHA256CryptoServiceProvider(), signature);
        }
    }
}

使用示例:

public class Application
{
    public static void Main()
    {
        LicenseGenerator licenseGenerator = new LicenseGenerator();
        LicenseValidator licenseValidator = new LicenseValidator();

        // 服务端生成License
        string generatedLicense = licenseGenerator.GenerateLicense();

        // 客户端输入License
        Console.Write("请输入License:");
        string userEnteredLicense = Console.ReadLine();

        // 客户端验证License
        if (licenseValidator.ValidateLicense(userEnteredLicense))
        {
            Console.WriteLine("License验证通过,软件已启动。");
            // 软件正常运行逻辑...
        }
        else
        {
            Console.WriteLine("License验证失败,无法启动软件。");
        }
    }
}

上述代码演示了使用RSA非对称加密进行License的生成和验证。上只是提供一个思路,在实际应用中,公钥和私钥需要安全存储,以确保系统的安全性。

 

标签:正版,string,License,验证,RSA,rsa,公钥,软件
From: https://www.cnblogs.com/hanbing81868164/p/18016898

相关文章

  • 【国产化】禁止使用不安全的密码算法:DES、RC2,RSA(1024位及以下),MD5,SHA1
    一、引言随着互联网的普及和技术的发展,网络安全问题日益严重。密码算法作为网络安全的基石,其安全性直接关系到用户数据的安全。一些不安全的密码算法不断被曝光,给用户带来了极大的安全隐患。二、不安全的密码算法1.DESDES(DataEncryptionStandard)是一种对称加密算法,自1977年......
  • P7031 [NWRRC2016] Anniversary Cake 题解
    作者还在想,居然没什么人写红题题解???咳咳。言归正传。本题没有想象中的那么复杂,咱分类讨论就行了。·若在属于蛋糕的平面直角坐标系中,两支蜡烛的横、纵轴不同,就会有多种切法。如图:           这样,我们随便找一种情况输出就行,反正有SpecialJudge......
  • github 代码使用注意事项 开源协议 license
    MIT许可证:只为作者保留版权,而无任何其他了限制。它使人们几乎可以对您的项目进行任何操作,即时是制作和分发封闭源代码版本。Babel,.NETCore和Rails使用MIT许可证¹。BSD许可证:同样鼓励代码共享和尊重原作者的著作权,同样允许代码修改,再发布(作为开源或商业软件)。需要满足......
  • HMAC-SHA256和RSA-SHA256签名有什么区别?
    HMAC-SHA256和RSA-SHA256都是常见的签名算法,它们都使用SHA-256哈希函数,但它们在签名方式和应用场景上有所不同。HMAC-SHA256:HMAC(Hash-basedMessageAuthenticationCode)是一种基于哈希函数的消息认证码。它使用一个密钥(共享密钥)和消息作为输入,生成一个固定长度的哈希值作......
  • Wolfram Mathematica 14.0 macOS Universal - 现代科学计算
    WolframMathematica14.0macOSUniversal-现代科学计算全球现代技术计算的终极系统请访问原文链接:https://sysin.org/blog/mathematica/,查看最新版。原创作品,转载请保留出处。作者主页:sysin.orgWolframMathematica全球现代技术计算的终极系统可在桌面、云端和移动中使......
  • java非对称加密,rsa
     1.甲方构建密钥对儿,将公钥公布给乙方,将私钥保留。2.甲方使用私钥加密数据,然后用私钥对加密后的数据签名,发送给乙方签名以及加密后的数据;乙方使用公钥、签名来验证待解密数据是否有效,如果有效使用公钥对数据解密。3.乙方使用公钥加密数据,向甲方发送经过加密后的数据;甲方获得加......
  • 学易云rsa
    importrequestsimportsubprocessfromfunctoolsimportpartialsubprocess.Popen=partial(subprocess.Popen,encoding="utf-8")importexecjscookies={'Hm_lvt_f66e994ea650146b85e157a2ad96dad8':'1706088220','......
  • The 2nd Universal Cup. Stage 19: Estonia J
    首先二分答案\(0/1\)分数规划是直接的,之后这题有一个非常反直觉的结论是直接忽略掉关于血量时刻\(\geqslant0\)的限制,仅仅要求最终血量\(\geqslant0\),改造问题与原问题等价。感性理解一下就是中间过程有\(<0\)但最终\(\geqslant0\)的卡特兰式增长速率其实是小于仅要求......
  • Binary tree traversal-- beadth-first and depth-first【1月23日学习笔记】
    点击查看代码//Binarytreetraversal--beadth-firstanddepth-first#include<iostream>#include<queue>//STLusingnamespacestd;structnode{intdata;node*left,*right;};node*getnewnode(intx){node*temp=newnode;temp-&......
  • Binary tree traversal-- level-order traversal using queue【1月23日学习笔记】
    点击查看代码//Binarytreetraversal--level-ordertraversalusingqueue#include<iostream>#include<queue>//STLusingnamespacestd;structnode{intdata;node*left,*right;};node*getnewnode(intx){node*temp=newnode;t......