c# 读取硬件信息并进行加密绑定

文章目录

    • 声明
    • 流程
      • 1.读取硬件信息
      • 2.加密
      • 3. 解密

声明

如果你也有兴趣或者想找作者聊聊,欢迎留言或发送邮件至:[email protected]
作者还拥有个人公众号,会写一些管理、感悟类文章,知圈,自创立以来一直保持着高质量(后台统计每篇的阅读完成率都在90%以上)持续更新,二维码如下,欢迎扫描关注:
c# 读取硬件信息并进行加密绑定_第1张图片

流程

  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,需要的可以下载参考

你可能感兴趣的:(C#)