Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
KEY = "12345678901234567890123456789012"
IV= "00000000000000000000000000000000" //you can add I.V
STRING: "12345678901234567890123456789012"
OUTPUT: "660813F92BF7D14F524F0E58FB28F9DE"<pre>

What I have tried:

<pre>Public Function Decrypt_AES(ByVal data As Byte(), ByVal key As Byte(), ByVal iv As Byte()) As Byte()
        Using AES128_ECB = Aes.Create()
            AES128_ECB.KeySize = 128
            AES128_ECB.BlockSize = 128
            AES128_ECB.Padding = PaddingMode.Zeros
            AES128_ECB.Key = AES_key
            AES128_ECB.IV = AES_IV

            Using decryptor = AES128_ECB.CreateDecryptor(AES128_ECB.Key, AES128_ECB.IV)
                Using ms = New MemoryStream()
                    Using cryptoStream = New CryptoStream(ms, decryptor, CryptoStreamMode.Write)
                        cryptoStream.Write(data, 0, data.Length)
                        cryptoStream.FlushFinalBlock()
                        Return ms.ToArray()
                    End Using
                End Using
            End Using
        End Using
    End Function


Public Function EncryptStringToBytes_Aes(ByVal data As Byte(), ByVal key As Byte(), ByVal iv As Byte()) As Byte()
        Dim encrypted As Byte()
        Using aesAlg As Aes = Aes.Create()
            aesAlg.Mode = CipherMode.CBC
            aesAlg.KeySize = 128
            aesAlg.BlockSize = 128
            aesAlg.FeedbackSize = 128
            aesAlg.Padding = PaddingMode.Zeros
            aesAlg.Key = key
            aesAlg.IV = iv

            Dim encryptor As ICryptoTransform = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV)

            Using msEncrypt As MemoryStream = New MemoryStream()
                Using csEncrypt As CryptoStream = New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)
                    csEncrypt.Write(data, 0, data.Length)
                    csEncrypt.FlushFinalBlock()
                    encrypted = msEncrypt.ToArray()
                End Using
            End Using

        End Using
        Return encrypted

    End Function
Posted
Updated 23-Mar-21 23:18pm
v5
Comments
Maciej Los 9-Mar-21 15:23pm    
Why output should be: "38413046323935314437380000000000"?
Nicky Ralph 9-Mar-21 19:14pm    
kindly refer to this website pls http://aes.online-domain-tools.com/
Input type: Text
Input text: ED02C5EDBCE71B35DB37CB403EBB47E8 (hex)
Function: AES
Mode: ECD
Key: 31323334353637387174617246523458
hex
Decrypt Output: 38413046323935314437380000000000

THAT should be the correct output.

1 solution

So many problems in such a small code sample!

For a start, Encoding.UTF8.GetBytes and Encoding.Unicode.GetBytes will return the bytes representing the characters in the specified string. But you are trying to convert a string of hexadecimal numbers to a byte array.
  • Input: ED02
  • Expected output: { 237, 2 }
  • Actual output (UTF8): { 69, 68, 48, 50 }
  • Actual output (Unicode): { 69, 0, 68, 0, 48, 0, 50, 0 }
Unicode - Wikipedia[^]

Secondly, Convert.ToBase64String will return a Base64 string representing the byte array you pass in. But you want to return a string containing the hexadecimal representation of the bytes you've passed in.
  • Input: { 56, 65, 48, 70 }
  • Expected output: 38413046
  • Actual output: OEEwRg==
Base64 - Wikipedia[^]

And thirdly, according to your comment, you want to DEcrypt the data. So why then are you creating an ENcryptor?!

You can't just call random methods that seem to take and return the correct types. You need to understand exactly what you're trying to do, and call the correct methods to do that.
VB.NET
Shared Function Decrypt(ByVal text As String) As String
    Dim aes As New AesCryptoServiceProvider()
    aes.BlockSize = 128
    aes.KeySize = 128
    aes.Key = HexStringToBytes(AesKey)
    aes.Mode = CipherMode.ECB
    aes.Padding = PaddingMode.None

    ' Convert string to byte array
    Dim src As Byte() = HexStringToBytes(text)

    Using enc As ICryptoTransform = aes.CreateDecryptor()
        Dim dest As Byte() = enc.TransformFinalBlock(src, 0, src.Length)
        Return BytesToHexString(dest)
    End Using
End Function

Shared Function HexStringToBytes(ByVal hexString As String) As Byte()
    Dim result As New List(Of Byte)()
    For i As Integer = 0 To hexString.Length - 1 Step 2
        result.Add(Convert.ToByte(hexString.Substring(i, 2), 16))
    Next
    Return result.ToArray()
End Function

Shared Function BytesToHexString(ByVal bytes As Byte()) As String
    Dim sb As New StringBuilder()
    For i As Integer = 0 To bytes.Length - 1
        sb.Append(bytes(i).ToString("X2"))
    Next
    Return sb.ToString()
End Function
 
Share this answer
 
v2
Comments
Nicky Ralph 13-Mar-21 3:55am    
Thank you for this Richars, this is really helpful. Maybe you can help me as welll with https://www.codeproject.com/Questions/5296767/HOW-to-DECRYPT-HEX-USING-3DES-in-VB-NET

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900