0% found this document useful (0 votes)
18 views2 pages

Visual Basic AES Decryption Example

VB Decryption Problem
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views2 pages

Visual Basic AES Decryption Example

VB Decryption Problem
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

I have a Python script that takes data off of a serial line and decodes it into several fields.

A
portion of the transmitted data is encrypted. I am having trouble decrypting the string.

When I call
ProcessMessage("01ee12f0d7a686b56f4679fc9b4d4dac703425b46d8fe793c62b83c7d17d9
edeb41a1ba22e"), I receive the following as a reply

AES Decrypted: ‫���__ی‬


_���l5�΄e�_(�Ml_���

I am expecting "0609139200ce00010003000193aa"

Visual Basic Code:

Public Sub ProcessMessage(sMsg As String)

Dim sEncryptVector As String = [Link](2, 16).Replace("-", "")


[Link]("Encrypt Vector: " + sEncryptVector)

Dim sEncryptedMsg As String = [Link](45, 29).Replace("-", "")


[Link]("Encrypted String: " + sEncryptedMsg)

Dim sBase64EncryptedMsg As String = ConvertStringToBase64(sEncryptedMsg)


[Link]("Base64 Encrypted String: " + sBase64EncryptedMsg)

Dim sKey As String = "ABABABABABABABABABABABABABABABAB"


Dim sDecrypted As String = DecryptString(sBase64EncryptedMsg, sKey,
sEncryptVector)
[Link]("AES Decrypted String : " + sDecrypted)

End Sub

Public Function DecryptString(encryptedText As String, key As String, iv As String) As


String
' Convert key and IV from string to byte arrays
Dim keyBytes As Byte() = [Link](key)
Dim ivBytes As Byte() = [Link](iv)

' Ensure the key and IV are the correct length for AES (e.g., 256-bit key = 32 bytes,
256-bit IV = 32 bytes)
If [Link] <> 32 Then
Throw New ArgumentException("Key must be 32 bytes (256 bits).")
End If
If [Link] <> 16 Then
Throw New ArgumentException("IV must be 16 bytes (128 bits).")
End If

' Convert the encrypted string back to a byte array


Dim encryptedBytes As Byte() = Convert.FromBase64String(encryptedText)

' Create an AES decryption object


Using aes As Aes = [Link]()
[Link] = keyBytes
[Link] = ivBytes
[Link] = [Link]
[Link] = [Link]

' Create a decryptor from the AES object


Using decryptor As ICryptoTransform = [Link]()
' Perform the decryption
Dim decryptedBytes As Byte() = [Link](encryptedBytes, 0,
[Link])

' Convert the decrypted bytes back to a string


Return [Link](decryptedBytes)
End Using
End Using
End Function

Public Function ConvertStringToBase64(inputString As String) As String


' Convert the string to a byte array using UTF8 encoding
Dim byteArray As Byte() = [Link](inputString)

' Convert the byte array to a Base64 string


Dim base64String As String = Convert.ToBase64String(byteArray)

' Return the Base64 string


Return base64String
End Function

You might also like