软件注册&注册码

using System;   
using System.Data;   
using System.Configuration;   
using System.Web;   
using System.Web.Security;   
using System.Web.UI;     
using System.Web.UI.WebControls;     
using System.Web.UI.WebControls.WebParts;     
using System.Web.UI.HtmlControls;     
using System.IO;     
using System.Security.Cryptography;     
using System.Management;   
  
/// <summary>     
/// Hasher 的摘要说明     
/// </summary>     
///      

namespace Hasher     
{     
public class Hasher     
{     
// private byte[] _HashKey; //哈希密钥存储变量    
public string _HashText; //待加密的字符串   
// public Hasher()    
// {    
// } 
  
// 需要产生加密哈希的字符串     
public string HashText   
{    
set  
{   
_HashText 
= value;   
}
   
get  
{   
return _HashText;   
}
   
}
   
    
/// 使用MD5CryptoServiceProvider类产生哈希值。不需要提供密钥。   
/// </summary>   
/// <returns></returns>   


public string MD5Hasher()   
{   
byte[] MD5Data = System.Text.Encoding.UTF8.GetBytes(HashText);   
MD5 Md5 
= new MD5CryptoServiceProvider();   
byte[] Result = Md5.ComputeHash(MD5Data);   
return Convert.ToBase64String(Result); //返回长度为字节字符串   
}
   
  

//获取cpu序列号   
public String GetCpuID()   
{   
ManagementClass mc 
= new ManagementClass("Win32_Processor");   
ManagementObjectCollection moc 
= mc.GetInstances();   
String strCpuID 
= null;   
foreach (ManagementObject mo in moc)   
{   
strCpuID 
= mo.Properties["ProcessorId"].Value.ToString();   
break;   
}
   
return strCpuID;   
}
//end method   
  
//获取硬盘序列号   
public String GetDriveID()   
{    
string driveSerialNum = "";    
Scripting.FileSystemObjectClass MySystem 
= new Scripting.FileSystemObjectClass();    
foreach (Scripting.Drive MyDriver in MySystem.Drives)   
{   
string SerialNumber = "";   
try  
{    
SerialNumber 
= MyDriver.SerialNumber.ToString();   
driveSerialNum 
= driveSerialNum + SerialNumber;   
}
   
catch (Exception ex)   
{   
   MessageBox.Show(ex.message);
}
    
}
   
return driveSerialNum;   
}
   
  

public String strJia()   
{   
string strJiami = "";   
strJiami 
= GetCpuID() + GetDriveID();   
return strJiami;   
}
   
  
public string jiqixuelie;   
public string duijiqima = null;   
///得到机器码   
public string hashGetDriveID()   
{   
Hasher hs 
= new Hasher();   
hs.HashText 
= hs.strJia();   
string jiqi = hs.MD5Hasher();   
duijiqima 
= jiqi.Substring(85);   
return duijiqima;   
}
   

public string Sern()    
//最终的序列号//m为配置文件值   
//x为序列号值     
//css进行比较该注册码是否正确   
{   
//888888888888888888888888888888888获取机器码   
Hasher hs = new Hasher();   
//88888888888888888888888888888888888888888888888888获取序列号   
Hasher hash = new Hasher();    
hash.HashText 
= hs.hashGetDriveID().ToString ();    
jiqixuelie 
= hash.MD5Hasher();   
return jiqixuelie;   
}
   
}
   
  
    
  
    
  
参考2   
  
using   System;      
using   System.Data;      
using   System.Data.OleDb;      
using   System.Text;      
using   System.Security.Cryptography;      
using   Microsoft.Win32;      
using   System.Configuration;   
  
  
///   <summary>      
  
///   加密数据     
  
///   </summary>      
  
///   <param   name="Text"></param>      
  
///   <param   name="sKey"></param>      
  
///   <returns></returns>      

  
  
private string EnText(string Text,string sKey)      
  
{      
  
         StringBuilder ret
=new StringBuilder();      
         
try    
         
{      
              DESCryptoServiceProvider des 
= new DESCryptoServiceProvider();       
              
byte[] inputByteArray;      
              inputByteArray
=Encoding.Default.GetBytes(Text);      
              
//通过两次哈希密码设置对称算法的初始化向量     
              des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile       
              (System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey,   
"md5").Substring(0,8),"sha1").Substring(0,8));      
  
              
//通过两次哈希密码设置算法的机密密钥      
              des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile      
              (System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey,   
"md5").Substring(0,8),"md5").Substring(0,8));      
              System.IO.MemoryStream ms
=new System.IO.MemoryStream();      
              CryptoStream cs
=new CryptoStream(ms,des.CreateEncryptor(),CryptoStreamMode.Write);      
              cs.Write(inputByteArray,
0,inputByteArray.Length);      
              cs.FlushFinalBlock();      
              
foreachbyte b in ms.ToArray())      
              
{       
                 ret.AppendFormat(
"{0:X2}",b);      
              }
      
              
return ret.ToString();      
         }
       
         
catch      
         
{      
              
return "";      
         }
      
  }
    
  
    
  
  
///   <summary>      
  
///   解密数据     
  
///   </summary>      
  
///   <param name="Text"></param>       
  
///   <param name="sKey"></param>      
  
///   <returns></returns>      

  private string DeText(string Text,string sKey)      
  
{      
         
try      
         
{       
              DESCryptoServiceProvider des 
= new DESCryptoServiceProvider();   //定义DES加密对象     
              int len;       
              len
=Text.Length/2;       
              
byte[] inputByteArray = new byte[len];       
              
int x,i;      
              
for(x=0;x<len;x++)      
              
{      
                   i 
= Convert.ToInt32(Text.Substring(x * 22),16);      
                   inputByteArray[x]
=(byte)i;      
              }
      
              
//通过两次哈希密码设置对称算法的初始化向量     
              des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile      
              (System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey,   
"md5").Substring(0,8),"sha1").Substring(0,8));      
              
//通过两次哈希密码设置算法的机密密钥     
              des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile      
              (System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey,   
"md5").Substring(0,8),"md5").Substring(0,8));      
              System.IO.MemoryStream ms
=new System.IO.MemoryStream();//定义内存流     
              CryptoStream cs=new CryptoStream(ms,des.CreateDecryptor(),CryptoStreamMode.Write);//定义加密流     
              cs.Write(inputByteArray,0,inputByteArray.Length);      
            cs.FlushFinalBlock();      
            
return Encoding.Default.GetString(ms.ToArray());      
         }
      
         
catch      
         
{      
            
return "";      
         }
      
  }
   
  
  
///   <summary>      
  
///   将加密的字符串转换为注册码形式     
  
///   </summary>      
  
///   <param   name="input">要加密字符串</param>       
  
///   <returns>装换后的字符串</returns>      

  public string transform(string input,string skey)      
  
{      
         
string transactSn=string.Empty;     
         
if(input ==   "")      
         
{      
            
return transactSn;      
         }
      
         
string initSn=string.Empty;      
         
try      
         
{      
              initSn
=this.EnText(this.EnText(input,skey),skey).ToString();
              transactSn
=initSn.Substring(0,5+ "-" +initSn.Substring(5,5+ "-" + initSn.Substring(10,5+ "-" + initSn.Substring(15,5+ "-"   +   initSn.Substring(20,5);        
            
return transactSn;      
         }
      
         
catch      
         
{      
            
return transactSn;      
         }
      
  }
  
posted @ 2009-08-19 02:30  錯咗  阅读(393)  评论(0编辑  收藏  举报