Hi,
I am trying to the AES encryption and deception working in my B4J app.
I thought I had this working in the past, but looks like I didn't get it fully working after all.
I have been told that the AES uses AES-128 in ECB mode.
I have been told the following .net code works:
The data I need to decrypt (which is sent from the product in the field) is sent to my B4J app as a UDP message.
I tried using the following B4J code to decrypt the message:
Using my B4J code above it shows the following error:
Based on the .Net code above, is the B4J code correct or have I done something wrong ?
I am guessing I have done something wrong since it's showing the above error, and I can't work it out.
I am trying to the AES encryption and deception working in my B4J app.
I thought I had this working in the past, but looks like I didn't get it fully working after all.
I have been told that the AES uses AES-128 in ECB mode.
I have been told the following .net code works:
B4X:
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
The data I need to decrypt (which is sent from the product in the field) is sent to my B4J app as a UDP message.
I tried using the following B4J code to decrypt the message:
B4X:
Private Sub RMSockUDP_PacketArrived (Packet As UDPPacket)
Decrypt(Packet.Data)
End Sub
Sub Decrypt(value() As Byte) As String
Try
Dim raw() As Byte = value
bb.Initialize
bb.Append(raw)
Dim binaryflag() As Byte = Array As Byte(0x33, 0x36, 0x37, 0x30, 0x42, 0x42, 0x39, 0x46, 0x2f,0x2f,0x31,0x2f,0x2f)
If bb.IndexOf(binaryflag) > -1 Then
Dim msgbytes() As Byte = bb.SubArray2(13,bb.IndexOf2(Array As Byte(0x0),13))
End If
kg.Initialize("AES")
kg.KeyFromBytes("0123456789ABCDEF".GetBytes("UTF8")) ' the key to encrypt the message was 0123456789ABCDEF
C.Initialize("AES/ECB/NoPadding")
msgbytes = C.Decrypt(msgbytes, kg.Key, False)
Return BConv.StringFromBytes(msgbytes, "UTF8").Trim
Catch
Log("Error: " & LastException.message)
End Try
End Sub
Using my B4J code above it shows the following error:
Error: java.lang.IllegalArgumentException: Null input buffer
Based on the .Net code above, is the B4J code correct or have I done something wrong ?
I am guessing I have done something wrong since it's showing the above error, and I can't work it out.