加解密作用和基本思路
在我们编写好一款软件后,我们不想别人盗用我们的软件,这时候我们可以采用注册的方式来保护我们的作品。这时候我们可能就需要简单了解一下加密解密技术,下面是我的简单总结:
第一步:程序获得运行机的唯一标示(比如:网卡号,CPU编号,硬盘号等等)。
第二步:程序将获得的唯一标示加密,然后有用户或者程序将加密后的标示发送给你。
第三步:你将加密后的标示解密(其实这时候你获得的就是:网卡号,CPU编号,硬盘号)然后你再将网卡号,CPU编号,硬盘号加密发送给客户注册。
第四步:程序将你发送的注册号进行解密,解密后的编号其实也是:网卡号,CPU编号,硬盘号。
第五步:每当程序启动,首先解密你发送的注册号,然后读取网卡号,CPU编号,硬盘号等等,最好进行验证,看两个标示是否一样。
获取硬件信息并加密信息
程序获得运行机的唯一标示:硬盘号,CPU信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| private string GetDiskID() { try { 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 { } }
private string GetCpuInfo() { try { string cpuInfo = ""; 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 ""; }
|
程序将获得的唯一标示加密
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| 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); }
|
解密注册信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| public static string Decrypt(string CypherText) { string KEY_64 = "haeren55"; string IV_64 = "HAEREN55"; 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 "无法解密!"; } }
|
自己测试代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
| #region 加密解密技术 string Secretkey; private string GetDiskID() { try { 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 { } } private string GetCpuInfo() { try { string cpuInfo = ""; 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 ""; } 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; System.IO.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 = "dafei250"; string IV_64 = "DAFEI500"; 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 "无法解密!"; } }
private void GetId_Click(object sender, EventArgs e) { tBoxID.Text = ""; Secretkey = GetDiskID() + GetCpuInfo(); UpdateTextBox(tBoxID, Secretkey); Secretkey = Encrypt(Secretkey); UpdateTextBox(tBoxID, Secretkey); }
private void senRegeditID_Click(object sender, EventArgs e) { UpdateTextBox(tBoxID, "解密:"); UpdateTextBox(tBoxID, Decrypt(Secretkey)); }
#endregion
|
相关链接(侵删)
- C#关于获取硬盘号、CPU信息,加密解密技术
=================我是分割线=================
欢迎到公众号来唠嗑: