Licensing实例(二)

本文档详细介绍了如何实现加密许可证的生成、验证和管理。包括使用DES加密算法、RSA公钥加密、哈希验证等技术,确保许可证的安全性和有效性。通过`EncryptedLicenseProvider`类的各个方法,如`GenerateKey`、`ExtractLicense`和`LoadLicense`,展示了许可证的生成、提取和验证过程。
摘要由CSDN通过智能技术生成
接上一篇:Licensing实例(一)
     /// <summary>
    
/// 加密许可证提供者
    
/// </summary>

     public   class  EncryptedLicenseProvider : LicenseProvider
    
{
        
private static byte[] _designSignature;
        
private static byte[] _desIV = new byte[] 173631981771144221161 };
        
private static byte[] _desKey = new byte[] 146215616118237179194 };
        
private static string _rsaParameters;
        
private static byte[] _runtimeSignature;
        
private static EncryptedLicense _systemLicense;
        
private const string _systemParameters = "";
        
private const int keyLength = 7;

        
public EncryptedLicenseProvider()
        
{
        }


        
/// <summary>
        
/// 数组匹配
        
/// </summary>
        
/// <param name="a1"></param>
        
/// <param name="a2"></param>
        
/// <returns></returns>

        private static bool ArrayEqual(byte[] a1, byte[] a2)
        
{
            
if (a1 != a2)
            
{
                
if ((a1 == null|| (a2 == null))
                
{
                    
return false;
                }

                
if (a1.Length != a2.Length)
                
{
                    
return false;
                }

                
for (int num1 = 0; num1 < a1.Length; num1++)
                
{
                    
if (a1[num1] != a2[num1])
                    
{
                        
return false;
                    }

                }

            }

            
return true;
        }


        
/// <summary>
        
/// 序列尺寸
        
/// </summary>
        
/// <param name="length"></param>
        
/// <returns></returns>

        private static int ArraySize(int length)
        
{
            
return length;
        }


        
/// <summary>
        
/// 提取许可证信息
        
/// </summary>
        
/// <param name="licenseKey">许可证键值</param>
        
/// <param name="password">密码</param>
        
/// <returns></returns>

        internal EncryptedLicense ExtractLicense(string licenseKey, string password)
        
{
            
try
            
{
                
//从哈希转到byte
                byte[] buffer1 = EncryptedLicenseProvider.FromHex(licenseKey);
                
byte[] buffer2 = new byte[] 62126142556816519363 };
                
//获取公钥标识
                byte[] buffer3 = Assembly.GetExecutingAssembly().GetName().GetPublicKeyToken();
                
if (!EncryptedLicenseProvider.ArrayEqual(buffer3, buffer2))
                
{
                    EncryptedLicenseProvider._desKey.CopyTo(buffer2, 
0);
                }

                DESCryptoServiceProvider provider1 
= new DESCryptoServiceProvider();
                provider1.Key 
= EncryptedLicenseProvider._desKey;
                provider1.IV 
= EncryptedLicenseProvider._desIV;
                
byte[] buffer4 = provider1.CreateDecryptor().TransformFinalBlock(buffer1, 0, buffer1.Length);
                
byte[] buffer5 = new byte[EncryptedLicenseProvider.ArraySize(8)];
                
byte[] buffer6 = new byte[EncryptedLicenseProvider.ArraySize(buffer4.Length - 7)];
                Array.Copy(buffer4, 
0, buffer5, 07);
                Array.Copy(buffer4, 
7, buffer6, 0, buffer6.Length);
                
byte[] buffer7 = EncryptedLicenseProvider.GetEncryptionKey(password);
                
if (!EncryptedLicenseProvider.ArrayEqual(buffer5, buffer7))
                
{
                    
return null;
                }

                provider1.IV 
= buffer5;
                
byte[] buffer8 = provider1.CreateDecryptor().TransformFinalBlock(buffer6, 0, buffer6.Length);
                
byte[] buffer9 = new byte[EncryptedLicenseProvider.ArraySize(buffer8.Length - 2)];
                Array.Copy(buffer8, 
2, buffer9, 0, buffer9.Length);
                
ushort num1 = BitConverter.ToUInt16(buffer8, 0);
                
string text1 = Encoding.UTF8.GetString(buffer9);
                
return new EncryptedLicense(licenseKey, num1, text1);
            }

            
catch
            
{
                
return null;
            }

        }


        
/// <summary>
        
/// 从哈希转到byte
        
/// </summary>
        
/// <param name="hex"></param>
        
/// <returns></returns>

        private static byte[] FromHex(string hex)
        
{
            
string text1 = EncryptedLicenseProvider.Strip(hex, "  -");
            
if ((text1 == null|| ((text1.Length % 2!= 0))
            
{
                
throw new FormatException("Invalid hexadecimal string");
            }

            
byte[] buffer1 = new byte[EncryptedLicenseProvider.ArraySize(text1.Length / 2)];
            
int num1 = 0;
            
for (int num2 = 0; num1 < text1.Length; num2++)
            
{
                
string text2 = text1.Substring(num1, 2);
                buffer1[num2] 
= byte.Parse(text2, NumberStyles.HexNumber);
                num1 
+= 2;
            }

            
return buffer1;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值