软件注册码(算法一DES)

DES,Rijndael,BouncyCastle
 1 /// <summary>
 2     /// 加密,解密 接口
 3     /// </summary>
 4     public interface ICrypto
 5     {
 6         /// <summary>
 7         /// 加密
 8         /// </summary>
 9         /// <param name="bytes"></param>
10         /// <returns></returns>
11         byte[] Encrypt(byte[] bytes);
12         /// <summary>
13         /// 解密
14         /// </summary>
15         /// <param name="bytes"></param>
16         /// <returns></returns>
17         byte[] Decrypt(byte[] bytes);
18     }
 1  /// <summary>
 2     /// DES算法
 3     /// </summary>
 4     public class DESCrypto : ICrypto
 5     {
 6         private byte[] _key;
 7         private byte[] _iv = new byte[] { 107, 22, 226, 145, 208, 243, 46, 41 };
 8         private DESCryptoServiceProvider GetProvider()
 9         {
10             DESCryptoServiceProvider descsp = new DESCryptoServiceProvider();
11             descsp.IV = this._iv;
12             descsp.Key = this._key;
13             return descsp;
14         }
15         public DESCrypto(byte[] key)
16         {
17             this._key = key;
18         }
19         /// <summary>
20         /// 加密
21         /// </summary>
22         /// <param name="bytes"></param>
23         /// <returns></returns>
24         public byte[] Encrypt(byte[] bytes)
25         {
26             DESCryptoServiceProvider descsp = this.GetProvider();
27             ICryptoTransform it = descsp.CreateEncryptor(descsp.Key, descsp.IV);
28             return it.TransformFinalBlock(bytes, 0, bytes.Length);
29         }
30         /// <summary>
31         /// 解密
32         /// </summary>
33         /// <param name="bytes"></param>
34         /// <returns></returns>
35         public byte[] Decrypt(byte[] bytes)
36         {
37             DESCryptoServiceProvider descsp = this.GetProvider();
38             ICryptoTransform it = descsp.CreateDecryptor(descsp.Key, descsp.IV);
39             return it.TransformFinalBlock(bytes, 0, bytes.Length);
40         }
41     }

1:建立LicenseInfo类,来完成软件的版本或者过期时间信息

 1  public class LicenseInfo
 2     {
 3         public byte LicenseType { get; set; }
 4         public int ExhaustionDayTicks { get; set; }
 5         public LicenseInfo()
 6         {
 7         }
 8         public LicenseInfo(byte licenseType, int exhaustionDayTicks)
 9         {
10             this.LicenseType = licenseType;
11             this.ExhaustionDayTicks = exhaustionDayTicks;
12         }
13         public byte[] ToByteArray()
14         {
15             byte[] bytes = new byte[5];
16             bytes[0] = LicenseType;
17             byte[] exhaustBytes = BitConverter.GetBytes(ExhaustionDayTicks);
18             for (int i = 0; i < 4; i++)
19             {
20                 bytes[i + 1] = exhaustBytes[i];
21             }
22             return bytes;
23         }
24         public static LicenseInfo FromByteArray(byte[] bytes)
25         {
26             LicenseInfo licenseInfo = new LicenseInfo();
27             licenseInfo.LicenseType = bytes[0];
28             licenseInfo.ExhaustionDayTicks = BitConverter.ToInt32(bytes, 1);
29             return licenseInfo;
30         }
31     }

 

 

2:建立Client客户端类,生成客户端序列号

 1 public byte[] GetProductKeyBytes(string pid)
 2         {
 3             string productKey = pid;
 4             byte[] bytes = Utils.GetBytes(productKey);
 5             byte[] hashBytes = Utils.ComputeHash(bytes);
 6             byte[] initHash = new byte[8] { 139, 59, 237, 93, 177, 134, 88, 69 };
 7             byte[] mergedHash = new byte[8];
 8             for (int i = 0; i < 8; i++)
 9             {
10                 mergedHash[i] = (byte)(initHash[i] ^ hashBytes[i] ^ hashBytes[i + 8]);
11             }
12             return mergedHash;
13         }
14         /// <summary>
15         /// 序列号
16         /// </summary>
17         /// <param name="pid"></param>
18         /// <returns></returns>
19         public string GetProductKey(string pid)
20         {
21             byte[] productKeyBytes = GetProductKeyBytes(pid);
22             return Utils.ConvertBytesToHexString(productKeyBytes);
23         }

 

3:软件制作者用客户端的序列号加密成注册码,发送给客户

4:客户验证注册码,进入软件

 1  /// <summary>
 2         /// 验证注册码
 3         /// </summary>
 4         /// <param name="productKey"></param>
 5         /// <param name="activationCode"></param>
 6         /// <param name="systemDate"></param>
 7         /// <param name="licenseInfo"></param>
 8         /// <returns></returns>
 9         public bool ValidateLicense(string productKey, string activationCode, DateTime systemDate, out LicenseInfo licenseInfo)
10         {
11             licenseInfo = null;
12             if (string.IsNullOrEmpty(productKey) || string.IsNullOrEmpty(activationCode)) return false;
13             byte[] productKeyBytes = Utils.ConvertHexStringToBytes(productKey);
14             byte[] activationCodeBytes = Utils.ConvertHexStringToBytes(activationCode);
15             licenseInfo = null;
16             try
17             {
18                 byte[] licenseInfoBytes = CryptoFactory.GetCrypto(productKeyBytes).Decrypt(activationCodeBytes);
19                 licenseInfo = LicenseInfo.FromByteArray(licenseInfoBytes);
20                 DateTime exhaustionDate = new DateTime(1900, 1, 1).AddDays(licenseInfo.ExhaustionDayTicks);
21                 return exhaustionDate > systemDate;
22             }
23             catch (Exception)
24             {
25                 return false;
26             }
27         }

 

posted @ 2013-05-10 22:03  微米大大  阅读(942)  评论(0编辑  收藏  举报