??本文介紹了基于 本機(jī)特征信息(如CPU、主板、BIOS和MAC等) 的軟件加密、授權(quán)與注冊(cè)方法,并分享了 示例程序完整源代碼。
1 加密、授權(quán)與注冊(cè)
??軟件的加密、授權(quán)與注冊(cè)流程如下:
2 本機(jī)特征信息
??本機(jī)特征信息主要包括:①CPU信息、②主板序列號(hào)、③BIOS信息、④MAC地址,基于C#獲取代碼如下:
using System;
using System.Management;
using System.Net.NetworkInformation;
using Microsoft.Win32;
namespace RWRegister
{
public class Computer
{
public static string ComputerInfo()
{
string info = string.Empty;
string cpu = GetCPUInfo();
string baseBoard = GetBaseBoardInfo();
string bios = GetBIOSInfo();
string mac = GetMACInfo();
info = string.Concat(cpu, baseBoard, bios, mac);
return info;
}
private static string GetCPUInfo()
{
string info = string.Empty;
info = GetHardWareInfo("Win32_Processor", "ProcessorId");
return info;
}
private static string GetBaseBoardInfo()
{
string info = string.Empty;
info = GetHardWareInfo("Win32_BaseBoard", "SerialNumber");
return info;
}
private static string GetBIOSInfo()
{
string info = string.Empty;
info = GetHardWareInfo("Win32_BIOS", "SerialNumber");
return info;
}
private static string GetMACInfo()
{
string info = string.Empty;
info = GetMacAddressByNetwork();
return info;
}
private static string GetHardWareInfo(string typePath, string Key)
{
try
{
ManagementClass mageClass = new ManagementClass(typePath);
ManagementObjectCollection mageObjectColl = mageClass.GetInstances();
PropertyDataCollection Properties = mageClass.Properties;
foreach (PropertyData property in Properties)
{
if (property.Name == Key)
{
foreach (ManagementObject mageObject in mageObjectColl)
{
return mageObject.Properties[property.Name].Value.ToString();
}
}
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return string.Empty;
}
private static string GetMacAddressByNetwork()
{
string Key = "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\";
string macAddress = string.Empty;
try
{
NetworkInterface[] Nics = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in Nics)
{
if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet && adapter.GetPhysicalAddress().ToString().Length != 0)
{
string fRegistryKey = Key + adapter.Id + "\\Connection";
RegistryKey rKey = Registry.LocalMachine.OpenSubKey(fRegistryKey, false);
if (rKey != null)
{
string fPnpInstanceID = rKey.GetValue("PnpInstanceID", "").ToString();
int fMediaSubType = Convert.ToInt32(rKey.GetValue("MediaSubType", 0));
if (fPnpInstanceID.Length > 3 && fPnpInstanceID.Substring(0, 3) == "PCI")
{
macAddress = adapter.GetPhysicalAddress().ToString();
for (int i = 1; i < 6; i++)
{
macAddress = macAddress.Insert(3 * i - 1, ":");
}
break;
}
}
}
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return macAddress;
}
}
}
3 加密與解密算法
??加密方法包括:①秘鑰加密(KeyA+keyB)、②MD5加密。基于C#的加密與解密算法如下:
using System;
using System.Security.Cryptography;
using System.Text;
using System.IO;
namespace RWRegister
{
public enum CryptoKey
{
KeyA, KeyB
}
public class Encryption
{
private string KeyA = "Sgg***";
private string KeyB = "Qing***";
private string MD5strBegin = "C++***";
private string MD5strEnd = "Matlab***";
private string ExecuteKey = string.Empty;
public Encryption()
{
InitKey();//KeyA
}
public Encryption(CryptoKey Key)
{
InitKey(Key);//Custom
}
private void InitKey(CryptoKey Key = CryptoKey.KeyA)
{
switch (Key)
{
case CryptoKey.KeyA:
ExecuteKey = KeyA;
break;
case CryptoKey.KeyB:
ExecuteKey = KeyB;
break;
}
}
public string EncryptOfKey(string str)
{
return Encrypt(str, ExecuteKey);
}
public string DecryptOfKey(string str)
{
return Decrypt(str, ExecuteKey);
}
public string EncryptOfMD5(string str)
{
str = string.Concat(MD5strBegin, str, MD5strEnd);//trim
MD5 md5 = new MD5CryptoServiceProvider();
byte[] fromdata = Encoding.Unicode.GetBytes(str);
byte[] todata = md5.ComputeHash(fromdata);
string MD5str = string.Empty;
foreach (var data in todata)
{
MD5str += data.ToString("x2");
}
return MD5str;
}
private string Encrypt(string str, string sKey)
{
DESCryptoServiceProvider DESCsp = new DESCryptoServiceProvider();
byte[] InputBytes = Encoding.Default.GetBytes(str);
DESCsp.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
DESCsp.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, DESCsp.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(InputBytes, 0, InputBytes.Length);
cs.FlushFinalBlock();
StringBuilder builder = new StringBuilder();
foreach (byte b in ms.ToArray())
{
builder.AppendFormat("{0:X2}", b);
}
builder.ToString();
return builder.ToString();
}
private string Decrypt(string pToDecrypt, string sKey)
{
DESCryptoServiceProvider DESCsp = new DESCryptoServiceProvider();
byte[] InputBytes = new byte[pToDecrypt.Length / 2];
for (int x = 0; x < pToDecrypt.Length / 2; x++)
{
int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
InputBytes[x] = (byte)i;
}
DESCsp.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
DESCsp.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, DESCsp.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(InputBytes, 0, InputBytes.Length);
cs.FlushFinalBlock();
return Encoding.Default.GetString(ms.ToArray());
}
}
}
4 軟件授權(quán)檢測(cè)
??軟件授權(quán)檢測(cè)代碼如下:
using System;
using System.Collections.Generic;
using System.IO;
namespace RWRegister
{
public class Register
{
public static string CryptographFile = "Cryptograph.Key";
public static string LicenseFile = "license.Key";
public static void WriteCryptographFile(string info)
{
WriteFile(info, CryptographFile);
}
public static void WriteLicenseFile(string info)
{
WriteFile(info, LicenseFile);
}
public static string ReadCryptographFile()
{
return ReadFile(CryptographFile);
}
public static string ReadLicenseFile()
{
return ReadFile(LicenseFile);
}
public static bool ExistCryptographFile()
{
return File.Exists(CryptographFile);
}
public static bool ExistLicenseFile()
{
return File.Exists(LicenseFile);
}
private static void WriteFile(string info, string fileName)
{
try
{
using (StreamWriter writer = new StreamWriter(fileName, false))
{
writer.Write(info);
writer.Close();
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
private static string ReadFile(string fileName)
{
string info = string.Empty;
try
{
using (StreamReader reader = new StreamReader(fileName))
{
info = reader.ReadToEnd();
reader.Close();
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return info;
}
public static bool CheckRegister()
{
//MD5 Cryptograph of Computer information
string CryptoInfo = Computer.ComputerInfo();
Encryption encryptor = new Encryption(CryptoKey.KeyA);
CryptoInfo = encryptor.EncryptOfKey(CryptoInfo);//KeyA
string MD5CryptoInfo = encryptor.EncryptOfMD5(CryptoInfo);//MD5
//judge the exist of Cryptograph file
if (!ExistCryptographFile() || CryptoInfo != ReadCryptographFile())
{
WriteCryptographFile(CryptoInfo);
}
//judge the exist of License file
if (!ExistLicenseFile())
{
return false;
}
//MD5 Cryptograph of License file
string MD5License = ReadLicenseFile();
encryptor = new Encryption(CryptoKey.KeyB);
MD5License = encryptor.DecryptOfKey(MD5License);//KeyB
if (MD5License == MD5CryptoInfo)//two md5 clash
{
return true;
}
else
{
return false;
}
}
}
}
5 示例程序
5.1 界面設(shè)計(jì)
??示例程序界面設(shè)計(jì)如下圖,包括:① “文件(F)” 菜單(包含“數(shù)據(jù)”、“退出”子菜單);② “幫助(H)” 菜單(包含“幫助文檔”、“軟件注冊(cè)”子菜單)。軟件開啟時(shí),將自動(dòng)檢測(cè)授權(quán)情況,若未授權(quán)僅有20秒的試用時(shí)間(20秒后將禁用軟件);若軟件未授權(quán),可按 “幫助”->“軟件注冊(cè)”步驟 進(jìn)行授權(quán)。
5.2 模塊代碼
??模塊代碼如下,包括窗體載入、軟件注冊(cè)等。文章來源:http://www.zghlxwxcb.cn/news/detail-404914.html
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
namespace RWRegister
{
public partial class softWare : Form
{
public static bool registerState = false;
private const int trialRunTime = 20;//seconds
public softWare()
{
InitializeComponent();
}
private void softWare_Load(object sender, EventArgs e)
{
MyTimer.Enabled = true;
MyTimer.Interval = 1000;
toolStripSLTime.Text = "";
//軟件授權(quán)檢測(cè)
if (Register.CheckRegister())
{
registerState = true;
}
else
{
softWareTrialRun();
}
}
/// <summary>
/// 試運(yùn)行軟件(后臺(tái)線程)
/// </summary>
private void softWareTrialRun()
{
Thread threadClose = new Thread(softWareClose);
threadClose.IsBackground = true;//后臺(tái)
threadClose.Start();
}
private void softWareClose()
{
int count = 0;
while (!registerState && count < trialRunTime)
{
if (registerState)
{
return;
}
Thread.Sleep(1000);//1 sec
count += 1;
}
if (registerState)
{
return;
}
else
{
Environment.Exit(0);
}
}
private void toolStripMExit_Click(object sender, EventArgs e)
{
Environment.Exit(0);
}
private void toolStripMRegister_Click(object sender, EventArgs e)
{
if (registerState)
{
MessageBox.Show("軟件已注冊(cè)授權(quán)!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
string fileName = string.Empty;
ofdLicense.Title = "打開許可文件";
ofdLicense.Filter = "許可文件(*.Key)|*.Key";
if (ofdLicense.ShowDialog() == DialogResult.OK)
{
fileName = ofdLicense.FileName;
}
else
{
return;
}
string localFileName = string.Concat(
Environment.CurrentDirectory,
Path.DirectorySeparatorChar,
Register.LicenseFile);
if (fileName != localFileName)
File.Copy(fileName, localFileName, true);
if (Register.CheckRegister())
{
registerState = true;
MessageBox.Show("注冊(cè)成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("許可文件非法!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void MyTimer_Tick(object sender, EventArgs e)
{
toolStripSLTime.Text = "本地時(shí)間:" + DateTime.Now.ToString();//北京時(shí)間
}
}
}
5.3 完整代碼下載
??文檔資源:基于C#的軟件加密、授權(quán)與注冊(cè) 貨真價(jià)實(shí)、童叟無欺,歡迎指導(dǎo)交流學(xué)習(xí)!文章來源地址http://www.zghlxwxcb.cn/news/detail-404914.html
到了這里,關(guān)于基于C#的軟件加密、授權(quán)與注冊(cè)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!