2012-07-12 59 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