用vb.net基于硬盘号和CPU序列号的软件加密技术代码
时间: 2024-03-21 14:41:38 浏览: 29
以下是基于硬盘号和CPU序列号的软件加密技术的VB.NET代码示例:
```
Imports System.Management
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim cpuInfo As String = String.Empty
Dim hddInfo As String = String.Empty
'获取CPU序列号
cpuInfo = GetCpuID()
'获取硬盘号
hddInfo = GetHddID()
'将CPU序列号和硬盘号组合成加密串
Dim encryptString As String = cpuInfo & hddInfo
'对加密串进行加密
Dim encryptedString As String = EncryptString(encryptString)
'将加密串保存到文件中
IO.File.WriteAllText("encrypt.txt", encryptedString)
'读取加密串
Dim readString As String = IO.File.ReadAllText("encrypt.txt")
'解密加密串
Dim decryptedString As String = DecryptString(readString)
'将解密串分解为CPU序列号和硬盘号
Dim cpu As String = decryptedString.Substring(0, 20)
Dim hdd As String = decryptedString.Substring(20)
'输出CPU序列号和硬盘号
MessageBox.Show("CPU序列号: " & cpu & vbCrLf & "硬盘号: " & hdd)
End Sub
'获取CPU序列号
Public Function GetCpuID() As String
Dim cpuInfo As String = String.Empty
Dim mc As ManagementClass = New ManagementClass("Win32_Processor")
Dim moc As ManagementObjectCollection = mc.GetInstances()
For Each mo As ManagementObject In moc
If cpuInfo = String.Empty Then
cpuInfo = mo.Properties("ProcessorId").Value.ToString()
End If
mo.Dispose()
Next
Return cpuInfo
End Function
'获取硬盘号
Public Function GetHddID() As String
Dim hddInfo As String = String.Empty
Dim mc As ManagementClass = New ManagementClass("Win32_DiskDrive")
Dim moc As ManagementObjectCollection = mc.GetInstances()
For Each mo As ManagementObject In moc
If hddInfo = String.Empty Then
hddInfo = mo.Properties("Model").Value.ToString()
End If
mo.Dispose()
Next
Return hddInfo
End Function
'加密字符串
Public Function EncryptString(ByVal str As String) As String
Dim key As String = "myKey123"
Dim data As Byte() = System.Text.Encoding.Unicode.GetBytes(str)
Dim result As Byte() = Nothing
Using aes As New System.Security.Cryptography.AesCryptoServiceProvider()
aes.Key = System.Text.Encoding.Unicode.GetBytes(key)
aes.IV = New Byte(15) {}
Dim encryptor As System.Security.Cryptography.ICryptoTransform = aes.CreateEncryptor()
result = encryptor.TransformFinalBlock(data, 0, data.Length)
End Using
Return Convert.ToBase64String(result)
End Function
'解密字符串
Public Function DecryptString(ByVal str As String) As String
Dim key As String = "myKey123"
Dim data As Byte() = Convert.FromBase64String(str)
Dim result As Byte() = Nothing
Using aes As New System.Security.Cryptography.AesCryptoServiceProvider()
aes.Key = System.Text.Encoding.Unicode.GetBytes(key)
aes.IV = New Byte(15) {}
Dim decryptor As System.Security.Cryptography.ICryptoTransform = aes.CreateDecryptor()
result = decryptor.TransformFinalBlock(data, 0, data.Length)
End Using
Return System.Text.Encoding.Unicode.GetString(result)
End Function
End Class
```
上述代码中,`GetCpuID`函数用于获取CPU序列号,`GetHddID`函数用于获取硬盘号,`EncryptString`函数用于加密字符串,`DecryptString`函数用于解密字符串。在`Form1_Load`事件中,将获取到的CPU序列号和硬盘号组合成加密串,对加密串进行加密并保存到文件中,然后读取加密串并解密,最后输出CPU序列号和硬盘号。您可以根据需要修改代码以满足您的具体需求。
相关推荐
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)