Click here to Skip to main content
15,303,825 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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)
                        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)
                    encrypted = msEncrypt.ToArray()
                End Using
            End Using

        End Using
        Return encrypted

    End Function
Updated 23-Mar-21 23:18pm
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
Input type: Text
Input text: ED02C5EDBCE71B35DB37CB403EBB47E8 (hex)
Function: AES
Mode: ECD
Key: 31323334353637387174617246523458
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.
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))
    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
    Return sb.ToString()
End Function
Nicky Ralph 13-Mar-21 3:55am
Thank you for this Richars, this is really helpful. Maybe you can help me as welll with

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