iOS Question AES Encryption problem

QtechLab

Active Member
Licensed User
Longtime User
Hi Everyone,

I've a big problem with the iEncryption library.

I need to bring this vb.net code on B4I.

B4X:
 Dim AES As New System.Security.Cryptography.RijndaelManaged

    Public Function Codifica(ByVal stringToEncrypt As String, Optional ByVal key() As Byte = Nothing) As String

        Dim encrypted As String = ""

        Try
            If key IsNot Nothing Then AES.Key = key
            AES.KeySize = 128
            AES.BlockSize = 128
            AES.Padding = Security.Cryptography.PaddingMode.Zeros
            AES.Mode = Security.Cryptography.CipherMode.CBC

            If AES.Key.Length > 0 Then
                Dim DESEncrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateEncryptor
                Dim Buffer As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(stringToEncrypt)
                encrypted = Convert.ToBase64String(DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
                'For temp2 As Byte = 0 To Buffer.Length - 1
                '    Debug.Print(String.Format("{0} - {1}", Buffer(temp2), Hex(Buffer(temp2))))
                'Next

            End If

        Catch ex As Exception
            Debug.Print(ex.Message)
        End Try

        Return encrypted

    End Function


This is my B4I Sub and it doesn't give me the right encrypted bytes, which are returned correctly in .NET.

B4X:
Private Sub GetXpKeys()

    Dim bytes2Crypt() As Byte = Array As Byte(TCPIP_KEY_REQUEST)
    Dim msg2Send() As Byte = iAes.Encrypt2(bytes2Crypt, pKeys, "AES", Null, iAes.OPTION_PKCS7Padding)

    SendMessage(msg2Send)
   
End Sub


Please help me, i've to solve this problem if i want to keep my job safe
 

QtechLab

Active Member
Licensed User
Longtime User
Hi EveryOne, Finally i found the issue!!!

I was trying to decrypt the buffer full sized without any joy.
I tryed to decrypt small portion of the buffer, 16 bytes per operation. It works!!

I don't know why, but if i divide the buffer in blocks of 16 bytes it can convert every piece without problems...

This is my decrypt code (code portion of the Event NEW_DATA of a AsyncStream object):

B4X:
Dim blocks As Byte = Buffer.Length / 16
Dim tmpByte(16) As Byte
Dim a As Int
Dim deBuffer(16*blocks) As Byte
               
For a=0 To blocks -1
        ByteConv.ArrayCopy(Buffer, 16*a, tmpByte,0 ,16)
        
        tmpByte = iAes.Decrypt2(tmpByte, pKeys, "AES", IV, 0)

        ByteConv.ArrayCopy(tmpByte, 0, deBuffer, 16*a, 16)
Next
 
Upvote 0
Top