VB .net输出中的AES加密与网站不匹配

时间:2013-04-30 22:19:04

标签: vb.net encryption aes

我正在尝试在VB .net上使用匹配版本的嵌入式平台上实现AES加密。嵌入式平台具有硬件AES加速,并给出与http://testprotect.com/appendix/AEScalc相同的结果。我已经搜索过并使用以下VB .net代码尝试执行相同操作但得到的结果不同。

    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中我做错了什么?感谢。

1 个答案:

答案 0 :(得分: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