1. 程式人生 > >c# 讀取硬體資訊並進行加密繫結

c# 讀取硬體資訊並進行加密繫結

流程

  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; }

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;
        }

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;
        }

此程式的加密程式是單獨的應用,有介面,輸入硬體字元資訊後會產生加密後的資料。而且程式碼量很小,已上傳至csdn,需要的可以下載參考