2012-07-12 101 views
1

我正在創建一個應用程序來創建每臺計算機都獨有的密鑰。此信息來源於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

+0

您在上面的鏈接被刪除,無法獲取頁面... – 2012-07-12 07:26:54

回答

0

這裏一個檢索處理器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 
+0

感謝蘭迪。如果我散列一個字符串,我可以'取消'它得到原始字符串? – 2012-07-14 05:00:44

+0

哈希函數是不可逆的。 – 2012-07-14 15:57:40