0% found this document useful (0 votes)
10 views

VB Decryption Problem

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)
10 views

VB Decryption Problem

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
You are on page 1/ 2

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 = sMsg.Substring(2, 16).Replace("-", "")


Debug.WriteLine("Encrypt Vector: " + sEncryptVector)

Dim sEncryptedMsg As String = sMsg.Substring(45, 29).Replace("-", "")


Debug.WriteLine("Encrypted String: " + sEncryptedMsg)

Dim sBase64EncryptedMsg As String = ConvertStringToBase64(sEncryptedMsg)


Debug.WriteLine("Base64 Encrypted String: " + sBase64EncryptedMsg)

Dim sKey As String = "ABABABABABABABABABABABABABABABAB"


Dim sDecrypted As String = DecryptString(sBase64EncryptedMsg, sKey,
sEncryptVector)
Debug.WriteLine("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() = Encoding.UTF8.GetBytes(key)
Dim ivBytes As Byte() = Encoding.UTF8.GetBytes(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 keyBytes.Length <> 32 Then
Throw New ArgumentException("Key must be 32 bytes (256 bits).")
End If
If ivBytes.Length <> 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 = Aes.Create()
aes.Key = keyBytes
aes.IV = ivBytes
aes.Mode = CipherMode.CFB
aes.Padding = PaddingMode.None

' Create a decryptor from the AES object


Using decryptor As ICryptoTransform = aes.CreateDecryptor()
' Perform the decryption
Dim decryptedBytes As Byte() = decryptor.TransformFinalBlock(encryptedBytes, 0,
encryptedBytes.Length)

' Convert the decrypted bytes back to a string


Return Encoding.UTF8.GetString(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() = Encoding.UTF8.GetBytes(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