c# 讀取硬件信息並進行加密綁定


 

聲明

如果你也有興趣或者想找作者聊聊,歡迎留言或發送郵件至:dreamdonghui@163.com
作者還擁有個人公眾號,會寫一些管理、感悟類文章,知圈,自創立以來一直保持着高質量(后台統計每篇的閱讀完成率都在90%以上)持續更新,二維碼如下,歡迎掃描關注:
知圈二維碼

流程

  1. 讀取硬件信息(此例中讀取cpu和磁盤信息)
  2. 加密
  3. 解密

注意:1.磁盤信息包括插入的移動硬盤或U盤,如果將此信息也綁定,那么插入外部存儲設備比如U盤的時候會誤導加密程序。2.加密和解密采用通用的加密算法,需要添加用戶自己的字段參與運算以增加加密可靠性,此例程中采用helloword字段參與加密運算,當然解密必須要采用與加密完全相同的字段。

1.讀取硬件信息

所需命名空間:using System.Management;
代碼段:

        private string GetSystemInfo() { ManagementClass mangnmt = new ManagementClass("Win32_LogicalDisk"); ManagementObjectCollection mcol = mangnmt.GetInstances(); string dskInfo = ""; foreach (ManagementObject strt in mcol) { dskInfo += Convert.ToString(strt["VolumeSerialNumber"]); } string cpuInfo = string.Empty; ManagementClass mc = new ManagementClass("win32_processor"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { cpuInfo = mo.Properties["processorID"].Value.ToString(); break; } return cpuInfo + dskInfo; } 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

2.加密

命名空間:using System.Security.Cryptography;
代碼示例:

        private string Encrypt(string clearText) { string EncryptionKey = "helloword"; byte[] clearBytes = Encoding.Unicode.GetBytes(clearText); using (Aes encryptor = Aes.Create()) { Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 }); encryptor.Key = pdb.GetBytes(32); encryptor.IV = pdb.GetBytes(16); using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write)) { cs.Write(clearBytes, 0, clearBytes.Length); cs.Close(); } clearText = Convert.ToBase64String(ms.ToArray()); } } return clearText; } 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

3. 解密

命名空間:using System.Security.Cryptography;
示例代碼:

        private string Decrypt(string cipherText) { string EncryptionKey = "helloword"; cipherText = cipherText.Replace(" ", "+"); try { Convert.FromBase64String(cipherText); } catch(Exception e) { MessageBox.Show("The license code is not available!!!"); return ""; } byte[] cipherBytes = Convert.FromBase64String(cipherText); using (Aes encryptor = Aes.Create()) { Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 }); encryptor.Key = pdb.GetBytes(32); encryptor.IV = pdb.GetBytes(16); using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write)) { cs.Write(cipherBytes, 0, cipherBytes.Length); cs.Close(); } cipherText = Encoding.Unicode.GetString(ms.ToArray()); } } return cipherText; } 
  • 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

此程序的加密程序是單獨的應用,有界面,輸入硬件字符信息后會產生加密后的數據。而且代碼量很小,已上傳至csdn,需要的可以下載參考

https://blog.csdn.net/dreamdonghui/article/details/84989923?utm_medium=distribute.pc_relevant.none-task-blog-title-1&spm=1001.2101.3001.4242


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM