C# 获取硬盘号,CPU信息,加密解密技术的步骤
在我们编写好一款软件后,我们不想别人盗用我们的软件,这时候我们可以采用注册的方式来保护我们的作品。这时候我们可能就需要简单了解一下加密解密技术,下面是我的简单总结:
第一步:程序获得运行机的唯一标示(比如:网卡号,CPU编号,硬盘号等等)。
第二步:程序将获得的唯一标示加密,然后有用户或者程序将加密后的标示发送给你。
第三步:你将加密后的标示解密(其实这时候你获得的就是:网卡号,CPU编号,硬盘号)然后你再将网卡号,CPU编号,硬盘号加密发送给客户注册。
第四步:程序将你发送的注册号进行解密,解密后的编号其实也是:网卡号,CPU编号,硬盘号。
第五步:每当程序启动,首先解密你发送的注册号,然后读取网卡号,CPU编号,硬盘号等等,最好进行验证,看两个标示是否一样。
具体实例看代码:
第一步:程序获得运行机的唯一标示:硬盘号,CPU信息
//获取硬盘号<script type="text/JavaScript"> alimama_pid="mm_10249644_1605763_5018464"; alimama_type="f"; alimama_sizecode ="tl_1x1_8"; alimama_fontsize=12; alimama_bordercolor="FFFFFF"; alimama_bgcolor="FFFFFF"; alimama_titlecolor="0000FF"; alimama_underline=0; alimama_height=22; alimama_width=0; </script> <script src="http://a.alimama.cn/inf.js" type=text/javascript> </script>
private string GetDiskID()
{
try
{
//获取硬盘ID
String HDid = "";
ManagementClass mc = new ManagementClass("Win32_DiskDrive");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
HDid = (string)mo.Properties["Model"].Value;
}
moc = null;
mc = null;
return HDid;
}
catch
{
return "";
}
finally
{
}
}
//获取CPU信息
private string GetCpuInfo()
{
try
{
string cpuInfo = "";//cpu序列号
ManagementClass cimobject = new ManagementClass("Win32_Processor");
ManagementObjectCollection moc = cimobject.GetInstances();
foreach (ManagementObject mo in moc)
{
cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
}
return cpuInfo;
}
catch
{
this.senRegeditID.Enabled = false;
this.GetId.Enabled = true;
}
return "";
}
第二步:程序将获得的唯一标示加密
//加密 <script type="text/JavaScript"> alimama_pid="mm_10249644_1605763_5027492"; alimama_type="f"; alimama_sizecode ="tl_1x5_8"; alimama_fontsize=12; alimama_bordercolor="FFFFFF"; alimama_bgcolor="FFFFFF"; alimama_titlecolor="0000FF"; alimama_underline=0; alimama_height=22; alimama_width=0; </script> <script src="http://a.alimama.cn/inf.js" type=text/javascript> </script>
static public string Encrypt(string PlainText)
{
string KEY_64 = "dafei250";
string IV_64 = "DAFEI500";
byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
int i = cryptoProvider.KeySize;
MemoryStream ms = new MemoryStream();
CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(cst);
sw.Write(PlainText);
sw.Flush();
cst.FlushFinalBlock();
sw.Flush();
return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
}
第三步:你将加密后的标示解密(注册的时候解密)
//解密
public static string Decrypt(string CypherText)
{
string KEY_64 = "haeren55"; //必须是8个字符(64Bit)
string IV_64 = "HAEREN55"; //必须8个字符(64Bit)
try
{
byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
byte[] byEnc;
try
{
byEnc = Convert.FromBase64String(CypherText);
}
catch
{
return null;
}
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream(byEnc);
CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cst);
return sr.ReadToEnd();
}
catch { return "无法解密!"; }
}
以上是 C# 获取硬盘号,CPU信息,加密解密技术的步骤 的全部内容, 来源链接: utcz.com/z/338935.html