1

我正在创建一个应用程序来创建每台计算机唯一的密钥。此信息来自操作系统序列号和处理器 ID。

有没有办法“缩短”字符串?也许通过将其转换为 HEX 或其他东西......

原因是这样的:我曾经使用 VB6 代码段 (http://www.planet-source-code.com/vb...48926&lngWId=1) 来获取详细信息,并且输出只有 13 位长。我的更长,但得到相同的信息......

顺便说一句,我在上面发布的链接获得了多个奖项,但我在将其转换为 .NET 时遇到了巨大的麻烦。有没有人有机会转换它,或者知道有人转换过它?还是真正有效的工具?

谢谢

编辑

这是完整的工作链接:http ://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=48926&lngWId=1

4

2 回答 2

2

听起来你想要一个“散列算法”或“散列函数”。它们是一个常见的概念:http ://en.wikipedia.org/wiki/Hash_function

一般来说,您可以简单地编写自己的函数来获取字符串并返回哈希数字,但这里有一些使用 .NET 框架的合适代码:http: //support.microsoft.com/kb/301053

于 2012-07-12T14:56:43.643 回答
0

这是一个工作示例,用于检索找到的第一个处理器的处理器 ID 和操作系统序列号;它将这些字符串连接在一起,然后对它们执行各种编码。

这是一个简单的 VB.Net 控制台项目。请务必在您的项目中引用System.Management程序集。只需将此代码示例复制并粘贴到主模块中,在 Sub Main() 的末尾设置一个断点,然后查看各种结果。

Module Module1

    Sub Main()
        Dim uniqueID As String = GetUniqueID()

        ' convert it to a base64 string
        Dim encoding As New Text.ASCIIEncoding()
        Dim result1 = Convert.ToBase64String(encoding.GetBytes(uniqueID))

        ' compress it
        Dim result2 As String = CompressString(uniqueID)
        Dim result3 As String = DecompressString(result2)

        ' encrypt it
        Dim result4 As String = AES_Encrypt(uniqueID, "password")
        Dim result5 As String = AES_Decrypt(result4, "password")

        ' hash it
        Dim result6 As String = HashString(uniqueID)
    End Sub

    Private Function GetUniqueID() As String
        Dim result As String = GetOSSerialNumber()
        Dim processorIDs() As String = GetProcessorIDs()
        If ((processorIDs IsNot Nothing) AndAlso (processorIDs.Count > 0)) Then
            result &= processorIDs(0)
        End If
        Return result
    End Function

    Private Function GetProcessorIDs() As String()
        Dim result As New List(Of String)
        Dim searcher = New System.Management.ManagementObjectSearcher("Select ProcessorId from Win32_Processor")
        For Each managementObj In searcher.Get()
            result.Add(CStr(managementObj.Properties("ProcessorId").Value))
        Next
        Return result.ToArray()
    End Function

    Private Function GetOSSerialNumber() As String
        Dim result As String = ""
        Dim searcher = New System.Management.ManagementObjectSearcher("Select SerialNumber from Win32_OperatingSystem")
        For Each managementObj In searcher.Get()
            result = CStr(managementObj.Properties("SerialNumber").Value)
        Next
        Return result
    End Function

    Public Function CompressString(ByVal source As String) As String
        Dim result As String = ""
        Dim encoding As New Text.ASCIIEncoding()
        Dim bytes() As Byte = encoding.GetBytes(source)
        Using ms As New IO.MemoryStream
            Using gzsw As New System.IO.Compression.GZipStream(ms, IO.Compression.CompressionMode.Compress)
                gzsw.Write(bytes, 0, bytes.Length)
                gzsw.Close()
                result = Convert.ToBase64String(ms.ToArray)
            End Using
        End Using
        Return result
    End Function

    Public Function DecompressString(ByVal source As String) As String
        Dim result As String = ""
        Dim bytes() As Byte = Convert.FromBase64String(source)
        Using ms As New IO.MemoryStream(bytes)
            Using gzsw As New System.IO.Compression.GZipStream(ms, IO.Compression.CompressionMode.Decompress)
                Dim data(CInt(ms.Length)) As Byte
                gzsw.Read(data, 0, CInt(ms.Length))
                Dim encoding As New Text.ASCIIEncoding()
                result = encoding.GetString(data)
            End Using
        End Using
        Return result
    End Function

    Public Function AES_Encrypt(ByVal input As String, ByVal pass As String) As String
        Dim AES As New System.Security.Cryptography.RijndaelManaged
        Dim Hash_AES As New System.Security.Cryptography.MD5CryptoServiceProvider
        Dim encrypted As String = ""
        Try
            Dim hash(31) As Byte
            Dim temp As Byte() = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass))
            Array.Copy(temp, 0, hash, 0, 16)
            Array.Copy(temp, 0, hash, 15, 16)
            AES.Key = hash
            AES.Mode = Security.Cryptography.CipherMode.ECB
            Dim DESEncrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateEncryptor
            Dim Buffer As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(input)
            encrypted = Convert.ToBase64String(DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
        Catch ex As Exception
        End Try
        Return encrypted
    End Function

    Public Function AES_Decrypt(ByVal input As String, ByVal pass As String) As String
        Dim AES As New System.Security.Cryptography.RijndaelManaged
        Dim Hash_AES As New System.Security.Cryptography.MD5CryptoServiceProvider
        Dim decrypted As String = ""
        Try
            Dim hash(31) As Byte
            Dim temp As Byte() = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass))
            Array.Copy(temp, 0, hash, 0, 16)
            Array.Copy(temp, 0, hash, 15, 16)
            AES.Key = hash
            AES.Mode = Security.Cryptography.CipherMode.ECB
            Dim DESDecrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateDecryptor
            Dim Buffer As Byte() = Convert.FromBase64String(input)
            decrypted = System.Text.ASCIIEncoding.ASCII.GetString(DESDecrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
        Catch ex As Exception
        End Try
        Return decrypted
    End Function

    Private Function HashString(ByVal source As String) As String
        Dim encoding As New Text.ASCIIEncoding()
        Dim bytes() As Byte = encoding.GetBytes(source)
        Dim workingHash() As Byte = New System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(bytes)
        Dim result As String = ""
        For Each b In workingHash
            result = result & b.ToString("X2")
        Next
        Return result
    End Function

End Module
于 2012-07-12T15:57:56.863 回答