2013-04-30 171 views
3

我試圖在VB .net中匹配版本的嵌入式平臺上實現AES加密。嵌入式平臺具有硬件AES加速功能,並給出了與http://testprotect.com/appendix/AEScalc相同的結果。我搜索了四周,並使用下面的VB.net代碼來嘗試做同樣的事情,但得到不同的結果。VB .net輸出中的AES加密與網站不匹配

Dim AES As New System.Security.Cryptography.RijndaelManaged 

    Dim key() As Byte = New Byte() {&HDE, &HAD, &HBE, &HEF, &HA5, &HF4, &H56, &H12, &HDE, &HAD, &HBA, &HAB, &H1, &H92, &H83, &H74} 

    AES.Key = key 
    AES.KeySize = 128 
    AES.BlockSize = 128 
    AES.Padding = Security.Cryptography.PaddingMode.None 
    AES.Mode = Security.Cryptography.CipherMode.ECB 
    Dim DESEncrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateEncryptor 

    Dim input() As Byte = New Byte() {&H12, &H34, &H56, &H78, &H9A, &HBC, &HDE, &HF0, &H24, &H68, &HAC, &HE0, &H78, &H94, &H56, &H12} 

    Dim enc() As Byte = DESEncrypter.TransformFinalBlock(input, 0, input.Length) 

我的輸入是:
鍵:0xdeadbeefa5f45612deadbaab01928374
輸入:0x123456789ABCDEF02468ACE078945612

通過網站給出的嵌入式平臺輸出:
0x2b9481a0f7b32f1088407d8834c3dc4c

輸出由VB .NET給出:
0x49ca99ee420a82acd72f1532141385fd

任何人都可以告訴我,我在做什麼錯誤的VB.net?謝謝。

+0

更新這一點。我注意到,每次運行VB時,我都會得到不同的答案。我不改變任何東西,只是建立和反覆運行。我也嘗試使用System.Security.Cryptography.AesManaged和cryptostream的東西,而不是轉換,但也得到錯誤(每次不同)結果 – AlexF 2013-05-14 11:50:53

+0

最後解決了感謝@vcsjones。事實證明,寫入KeySize會清除先前寫入的Key。 – AlexF 2013-05-16 14:42:57

回答

0

試試這個功能,希望這有助於

加密

Public Function AESEncryption(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)) 
      Return encrypted 
     Catch ex As Exception 
     End Try 
    End Function 

解密

Public Function AESDecryption(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)) 
      Return decrypted 
     Catch ex As Exception 
     End Try 
    End Function 

Source