1

各コンピューターに固有のキーを作成するアプリケーションを作成しています。この情報は、OS のシリアル番号とプロセッサ 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 Frameworkを使用する適切なコードがいくつかあります:http ://support.microsoft.com/kb/301053

于 2012-07-12T14:56:43.643 に答える
0

Here is a working example which retrieves the Processor ID, for the first Processor found, and the OS Serial Number; it concatenates these to strings together and then performs various encodings on them.

This is a simple VB.Net Console project. Be sure to reference the System.Management assembly in your project. Just copy and paste this code example into the main module, set a breakpoint at the end of Sub Main(), and look at the various results.

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 に答える