缩短包含数据的字符串

时间:2012-07-12 07:16:37

标签: vb.net vb.net-2010

我正在创建一个应用程序来创建每台计算机独有的密钥。此信息源自OS序列号和processorID。

有没有办法'缩短'一个字符串?也许通过将其转换为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

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

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

这是一个简单的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