3

我正在尝试在具有 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 中做错了什么?谢谢。

4

1 回答 1

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

资源

于 2013-06-01T12:53:59.500 回答