Home > front end >  VB NET AES encrypt
VB NET AES encrypt

Time:12-29

Can't reproduce an AES online encoder example using VB.Net

Trying in https://www.devglan.com/online-tools/aes-encryption-decryption with following parameters:

  • Text to be Encrypted: test
  • Cipher Mode: ECB
  • Key Size: 128
  • Secret Key: 1234567890123456

I get this output: 3fvaLg5IDlveswuXzhVQcw==

If I try in VB.Net using this function (found in https://gist.github.com/ShaneGowland/5973974):

    Public Shared 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 = 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
            Return ex.ToString
        End Try
    End Function

I get this output: 6mhZOr1dQ7PWqbRGzmMgjg== which is not matching with got at devglan.com

I tried with different paddings with no luck. What am I doing wrong?

PS: I am aware that the ECB method should not be used

CodePudding user response:

Working with this function:

Public Shared 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
        AES.Mode = CipherMode.ECB
        AES.Key = Encoding.UTF8.GetBytes(pass)

        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
        Return ex.ToString
    End Try
End Function
  • Related