0

I figured this would be pretty straight forward but I have an issue with getting my AES Encryption function to return a Hex String. I can get it to work when I convert it to Base64 but I cannot get the String with Hex values. Here is my code. Any help would be appreciated.

Dim AES_ENCRYPTION As New System.Security.Cryptography.RijndaelManaged
Dim CODE_AES As New System.Security.Cryptography.MD5CryptoServiceProvider
Dim encrypted As String = ""
Try
    Dim hash(31) As Byte
    Dim temp As Byte() = CODE_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass))
    Array.Copy(temp, 0, hash, 0, 16)
    Array.Copy(temp, 0, hash, 15, 16)
    AES_ENCRYPTION.Key = hash
    AES_ENCRYPTION.Mode = CipherMode.ECB
    Dim AES_ENCRYPTOR As System.Security.Cryptography.ICryptoTransform = AES_ENCRYPTION.CreateEncryptor
    Dim Buffer As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(input)
    encrypted = (Conversion.Hex(AES_ENCRYPTOR.TransformFinalBlock(Buffer, 0, Buffer.Length)))
Catch ex As Exception
End Try

Return encrypted
CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
Ty Jacobs
  • 113
  • 2
  • 7
  • 23
  • Pick an answer from [How do you convert Byte Array to Hexadecimal String, and vice versa?](http://stackoverflow.com/questions/311165/how-do-you-convert-byte-array-to-hexadecimal-string-and-vice-versa) and convert it to vb.net – CodesInChaos Jul 31 '13 at 18:41
  • 6
    I also want to note that your crypto is weak (ECB, no MAC, no IV) and the way you treat the key nonsensical. You don't use an appropriate salted and slow password hash, such as PBKDF2. I have no clue what your weird copying is supposed to achieve. You silently corrupt unicode data outside the ASCII range. – CodesInChaos Jul 31 '13 at 18:43
  • And that's why I came to StackOverFlow. This isn't a project or production code. It is something to play with. Thanks for the link. – Ty Jacobs Jul 31 '13 at 19:17
  • What output do you get when you try the Hex conversion? What does the expected output look like? What are the differences between the two? – rossum Aug 01 '13 at 15:28
  • The conversion does not work at all. The reason I need the output to be hex is because I wrote it in Jave to encrypt with hex output and decrypt the hex. – Ty Jacobs Aug 01 '13 at 19:24

1 Answers1

0

I've tried your example, and I've got nothing either.

So what I tried instead of having encrypted = (Conversion.Hex(AES_ENCRYPTOR.TransformFinalBlock(Buffer, 0, Buffer.Length))), I have used a loop to convert each byte to its hex equivalent, and concatenate it to encrypted.

Dim encrypted_byte() As Byte = AES_ENCRYPTOR.TransformFinalBlock(Buffer, 0, Buffer.Length)
For i As Integer = 0 To encrypted_byte.Length - 1
    encrypted = encrypted & Hex(encrypted_byte(i)).ToUpper
Next

I'm not sure how you formatted your hex string in Java, but this should be at least a start.

ki2ne
  • 78
  • 1
  • 1
  • 7