首先看下页面
代码如下
private void btn_Create_Click(object sender, EventArgs e)
{
string uuid = GetUuid();
string jiqima = GenerateMD5(uuid).ToUpper();
tb_IdCard.Text = jiqima;
}
/// <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";
}
}
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();
}
}
/// <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;
}
}
}
private void btn_Copy_Click(object sender, EventArgs e)
{
string text = tb_IdCard.Text.Trim();
if (text == "nuttertools")
{
label1.Visible = true;
label2.Visible = true;
tb_ShouQuanTime.Visible = true;
btn_CreateDat.Visible = true;
}
else
{
Clipboard.SetDataObject(text);
MessageBox.Show("复制成功,请联系开发者授权_QQ:1664701345");
}
}
private void btn_CreateDat_Click(object sender, EventArgs e)
{
try
{
string uuid = GetUuid();
string jiqima = GenerateMD5(uuid).ToUpper();
double days =double.Parse(tb_ShouQuanTime.Text.Trim());
DateTime today = DateTime.Now.AddDays(days);
string ConfigFilePath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "CCGiao.DAT");
//写入
Hashtable para = new Hashtable();
para.Add("jqm", jiqima);
para.Add("days", today);
EncryptUtilSeal.EncryptObject(para, ConfigFilePath);
MessageBox.Show("生成成功");
this.Close();
}
catch
{
MessageBox.Show("生成失败");
}
}
//获取当前程序级的版本号
public static Version GetEdition()
{
return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
}
private void tb_IdCard_TextChanged(object sender, EventArgs e)
{
string te = tb_IdCard.Text.Trim();
if (te == "nu")
{
tb_IdCard.PasswordChar = '*';
}
}
标签:string,para,生成器,机器码,static,new,csEncrypt,uuid
From: https://blog.51cto.com/u_16371710/8956214