' struct that holds the buffer and count of bytes in it and some other stuff
Public Structure XepQData_Class
Public EPProtocol As Protocol
Public EP As IPEndPoint
Public RmtTcpSess As XepTcpObject
Public RmtUdpClient As UdpClient
Public IOBuff() As Byte
Public NumBytesInBuff As Integer
End Structure
'define a variable from that struct:
Dim XepQData As New XepQData_Class
' code to pass the message to the aes decryption is as follows:
Dim crypt As New AES(AESkey)
crypt.Decrypt(XepQData.IOBuff, XepQData.NumBytesInBuff)
Public Class AES
Dim cAES As RijndaelManaged
Public Sub New(ByVal AESkey As Byte())
Dim InitVector() As Byte = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
cAES = New RijndaelManaged
cAES.BlockSize = 128
cAES.KeySize = 128
cAES.Mode = CipherMode.ECB
cAES.IV = InitVector
cAES.Padding = PaddingMode.Zeros
cAES.Key = AESkey
End Sub
Public Sub Decrypt(ByRef cBytes As Byte(), ByVal NumBytes As Integer)
Dim PlainBytes(IOBuffSize) As Byte
Dim i As Integer
Dim cDecryptor As ICryptoTransform = cAES.CreateDecryptor()
' Skip over the first 13 bytes - they are already in the plain.
For i = 13 To NumBytes - 16 Step 16
cDecryptor.TransformBlock(cBytes, i, 16, PlainBytes, i)
Next
Array.Copy(PlainBytes, 13, cBytes, 13, NumBytes - 13)
End Sub
Public Sub Encrypt(ByRef cBytes As Byte(), ByRef NumBytes As Integer)
Dim EncryptedBytes(IOBuffSize) As Byte
Dim i As Integer
' pad to a multiple of 16
Dim numPadBytes As Integer = 16 - ((NumBytes - 13) Mod 16)
ReDim Preserve cBytes(NumBytes + numPadBytes - 1)
For i = NumBytes To NumBytes + numPadBytes - 1
cBytes(i) = 0
Next i
NumBytes += numPadBytes
Dim cEncryptor As ICryptoTransform = cAES.CreateEncryptor()
' Skip over the first 13 bytes - they are left in the plain.
For i = 13 To NumBytes - 1 Step 16
cEncryptor.TransformBlock(cBytes, i, 16, EncryptedBytes, i)
Next
Array.Copy(EncryptedBytes, 13, cBytes, 13, NumBytes - 13)
End Sub
End Class