当自己做的小工具不想泛滥传播时,而自己又没有网络服务器,即可采用此种本地文件授权的方式,授权文件中包含授权时间和电脑的唯一识别编号
需要配合另一个机器码生成工具来使用
首先在Program-应用程序的主入口点中添加验证是否授权
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
GlobalData globalData = new GlobalData();
bool shouquan = globalData.ISAuthorizationot();
if (shouquan == true)
{
Application.Run(new FormMain());
}
else
{
MessageBox.Show("当前未授权");
}
}
globalData.ISAuthorizationot();代码如下:
/// <summary>
/// 是否已经授权
/// </summary>
/// <returns></returns>
public bool ISAuthorizationot()
{
try
{
string uuid = GetUuid();
string jiqima = GenerateMD5(uuid).ToUpper();
string ConfigFilePath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "CCGiao.DAT");
//读取
Hashtable para = new Hashtable();
object obj = EncryptUtilSeal.DecryptObject(ConfigFilePath);
para = obj as Hashtable;
string jqm = para["jqm"].ToString();
DateTime guoqidays =DateTime.Parse(para["days"].ToString());
DateTime daysnow = DateTime.Now;
if (jqm == jiqima && guoqidays>daysnow)
{
return true;
}
else
{
return false;
}
}
catch
{
return false;
}
}
以下是上个方法中需要用到的方法,是获取设备唯一编号的方法和MD5加密的方法
/// <summary>
/// 获取UUID
/// </summary>
/// <returns></returns>
public static string GetUuid()
{
string uuid = null;
try
{
SelectQuery query = new SelectQuery("select * from Win32_ComputerSystemProduct");
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
{
foreach (var item in searcher.Get())
{
using (item) uuid = item["UUID"].ToString();
}
}
//有些厂商的配件不支持UUId会显示为一堆F
if (uuid == null || uuid == "FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF")
{
uuid = GetMacAddress();
}
return uuid;
}
catch (Exception)
{
return "unknown";
}
}
/// <summary>
/// 获取本机MAC地址
/// </summary>
/// <returns>本机MAC地址</returns>
public static string GetMacAddress()
{
try
{
string strMac = string.Empty;
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if ((bool)mo["IPEnabled"] == true)
{
strMac = mo["MacAddress"].ToString();
}
}
moc = null;
mc = null;
return strMac;
}
catch
{
return "unknown";
}
}
/// <summary>
/// 对机器码进行MD5加密以此来保持长度一致
/// </summary>
/// <param name="txt"></param>
/// <returns></returns>
public static string GenerateMD5(string txt)
{
using (MD5 mi = MD5.Create())
{
byte[] buffer = Encoding.Default.GetBytes(txt);
//开始加密
byte[] newBuffer = mi.ComputeHash(buffer);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < newBuffer.Length; i++)
{
sb.Append(newBuffer[i].ToString("x2"));
}
return sb.ToString();
}
}
以下是C#读取和写入DAT文件的代码,需要授权的软件中只需要读取DAT文件,然后验证和当前机器码是否一致即可判断是否已经过授权
/// <summary>
/// 加密、解密
/// </summary>
public class EncryptUtilSeal
{
private static byte[] key = new byte[] { 78, 56, 61, 94, 12, 88, 56, 63, 66, 111, 102, 77, 1, 186, 97, 45 };
private static byte[] iv = new byte[] { 36, 34, 42, 122, 242, 87, 2, 90, 59, 117, 123, 63, 72, 171, 130, 61 };
private static IFormatter S_Formatter = null;
static EncryptUtilSeal()
{
S_Formatter = new BinaryFormatter();//创建一个序列化的对象
}
/// <summary>
/// 采用Rijndael128位加密二进制可序列化对象至文件
/// </summary>
/// <param name="para">二进制对象</param>
/// <param name="filePath">文件路径</param>
/// <returns></returns>
public static bool EncryptObject(object para, string filePath)
{
//创建.bat文件 如果之前错在.bat文件则覆盖,无则创建
using (Stream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
{
RijndaelManaged RMCrypto = new RijndaelManaged();
CryptoStream csEncrypt = new CryptoStream(fs, RMCrypto.CreateEncryptor(key, iv), CryptoStreamMode.Write);
S_Formatter.Serialize(csEncrypt, para);//将数据序列化后给csEncrypt
csEncrypt.Close();
fs.Close();
return true;
}
}
/// <summary>
/// 从采用Rijndael128位加密的文件读取二进制对象
/// </summary>
/// <param name="filePath">文件路径</param>
/// <returns>二进制对象</returns>
public static object DecryptObject(string filePath)
{
//打开.bat文件
using (Stream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
object para;
RijndaelManaged RMCrypto = new RijndaelManaged();
CryptoStream csEncrypt = new CryptoStream(fs, RMCrypto.CreateDecryptor(key, iv), CryptoStreamMode.Read);
para = S_Formatter.Deserialize(csEncrypt); //将csEncrypt反序列化回原来的数据格式;
csEncrypt.Close();
fs.Close();
return para;
}
}
}
原创帖子,请勿传播
标签:return,string,个人,验证,para,static,new,授权,csEncrypt From: https://blog.51cto.com/u_16371710/8944868