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
stringToEncrypt is an input string taken from a textbox, The code that converts the result to base 64 is internal to VB.NET...
This is the complete class, i can also use the "Codifica" Function that return the bytes Array.

I don't understand how to set the B4I parameters in order to get the same results

B4X:
Imports System.Text
Imports System.Security.Cryptography

Public Class ClsAES128Crypt

    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 mEncrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateEncryptor
                Dim Buffer As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(stringToEncrypt)
                encrypted = Convert.ToBase64String(mEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))

            End If

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

        Return encrypted

    End Function

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

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

        Dim Buffer As Byte() = Nothing
        Try

            If AES.Key.Length > 0 Then
                Dim mEncrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateEncryptor
                Buffer = mEncrypter.TransformFinalBlock(BytesToEncrypt, 0, BytesToEncrypt.Length)

            End If

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

        Return Buffer

    End Function

    Public Function Decodifica(ByVal stringToDencrypt As String, Optional ByVal key() As Byte = Nothing) As String

        Dim decrypted As String = ""

        Try

            If key IsNot Nothing Then AES.Key = key
            If AES.Key.Length > 0 Then
                Dim mDecrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateDecryptor
                'Dim Buffer As Byte() = Convert.FromBase64String(stringToDencrypt)
                Dim Buffer As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(stringToDencrypt)
                decrypted = System.Text.ASCIIEncoding.ASCII.GetString(mDecrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
            End If

        Catch ex As Exception

        End Try

        Return decrypted

    End Function

    Public Function Decodifica(ByVal BytesToDencrypt() As Byte, Optional ByVal key() As Byte = Nothing) As Byte()

        Dim Buffer As Byte() = Nothing

        Try

            If key IsNot Nothing Then AES.Key = key
            If AES.Key.Length > 0 Then
                Dim mDecrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateDecryptor
                Buffer = DESDecrypter.TransformFinalBlock(BytesToDencrypt, 0, BytesToDencrypt.Length)
            End If

        Catch ex As Exception

        End Try

        Return Buffer

    End Function

End Class


Thanks in advance
 
Upvote 0

QtechLab

Active Member
Licensed User
Longtime User
Here you are
B4X:
private Sub CryptXpKeys
   
    'iAES = iEncryption library object
   
    'TCPIP_KEY_REQUEST = 0x9
    'pkeys = Array As Byte(0x8a, 0x39, 0xd1, 0xc5, 0x26, 0x14, 0x3b, 0x4, 0x7f, 0x82, 0xca, 0x57, 0xa1, 0x3e, 0xc6, 0xbd)
    Dim bytes2Crypt() As Byte = Array As Byte(TCPIP_KEY_REQUEST)
    Dim msg2Send() As Byte = iAes.Encrypt2(bytes2Crypt, pKeys, "AES", Null, iAes.OPTION_PKCS7Padding)
   
    'Global flag that allow to check the phase of the transmission
    FaseRichiesta = EnumType.FaseComunicazione.RichiestaChiavi
   
    SendMessage(msg2Send)
   
End Sub
 
Upvote 0

QtechLab

Active Member
Licensed User
Longtime User
then i don't understand what goes in the place of IV. pKeys and TCPIP_KEY_REQUEST are global variable, they're initialized with the values that you can see in the comment
 
Upvote 0

QtechLab

Active Member
Licensed User
Longtime User
I'm a little bothered.
I can't explain why this library doesn't work properly...
Can you give me an example of AES-128?
I thought it was easy to use, such as other compilers.

I want a 16 bytes encrypted array after the operation with AES 128-bit encryption.

Well, i receive an array of 32 bytes, where the firsts 16 are the response that i'm looking for.

B4X:
Encrypt2(...) result:
<30ac6bb5 7731561a 26bcb8b5 2bf5cbda d748258b 0689900e 1d96d08d e523df23>

Vb.NET - C++ result:
<30ac6bb5 7731561a 26bcb8b5 2bf5cbda>

B4X:
public Sub GetXpKeys
   
    'TCPIP_KEY_REQUEST = 0x9
    'pkeys = Array As Byte(0x8A, 0x39, 0xD1, 0xC5, 0x26, 0x14, 0xEB, 0x04, 0x7F, 0x82, 0xCA, 0x57, 0xA1, 0x3E, 0xC6, 0xBD)
   
    Dim IV(16) As Byte = Array As Byte(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
    Dim bytes2Crypt(16) As Byte = Array As Byte(TCPIP_KEY_REQUEST, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)

    Dim msg2Send() As Byte = iAes.Encrypt2(bytes2Crypt, pKeys, "AES", IV, iAes.OPTION_PKCS7Padding)
   
    'Take firsts 16 bytes of the crypted message of 32 byte
    Dim bt As ByteConverter
    Dim msg2Send2(16) As Byte
    bt.ArrayCopy(msg2Send, 0, msg2Send2, 0, 16)
   
    'Global flag that allow to check the transmission phase
    FaseRichiesta = EnumType.FaseComunicazione.RichiestaChiavi
   
    SendMessage(msg2Send2)
   
End Sub
 
Upvote 0

QtechLab

Active Member
Licensed User
Longtime User
Hello Erel,
I'm back with another trouble...

This time i've some problems with decryption.

The device that i'm connecting to, have to dialog with encrypted data.
A function of it give me back a buffer of 80 "AES-encrypted" bytes.

I noticed that the decrypt function, decrypt correctly only 14 bytes and not all 80.
This is very strange... Every time i try the function i have the firsts 14 bytes that are decrypted correctly, and the others that are always different from the previous attempt.

I developed the same function for a VB.NET program and it work perfectly.

This is the row where i decrypt the buffer
B4X:
'pKeys is a dynamic calculated key
'IV : 16 bytes at 0 value
Dim deBuffer(80) As Byte = iAes.Decrypt2(Buffer, pKeys, "AES", IV, 0)

Dim i As Int = 0
For i=0 To Buffer.Length-1
[INDENT]Log("Byte n° " & i & " - " & deBuffer(i))[/INDENT]
Next

Dim name As String
For i=0 To 48-1
[INDENT]name = name & Chr(deBuffer(i+22))[/INDENT]
Next

Log ("Device Name : " & name)
Log("Buffer Length - " & Buffer.Length)

Log B4I
B4X:
iSock_Connected = true
Byte n° 0 - 1
Byte n° 1 - 0
Byte n° 2 - 2
Byte n° 3 - 8
Byte n° 4 - 0
Byte n° 5 - 2
Byte n° 6 - 54
Byte n° 7 - 0
Byte n° 8 - 0
Byte n° 9 - 0
Byte n° 10 - 0
Byte n° 11 - 0
Byte n° 12 - 0
Byte n° 13 - 56
'The bytes below are decrypted wrong
Byte n° 14 - 243
Byte n° 15 - 55
Byte n° 16 - 63
Byte n° 17 - 255
Byte n° 18 - 182
Byte n° 19 - 21
Byte n° 20 - 40
Byte n° 21 - 115
Byte n° 22 - 41
Byte n° 23 - 217
Byte n° 24 - 2
Byte n° 25 - 146
Byte n° 26 - 169
Byte n° 27 - 0
Byte n° 28 - 20
Byte n° 29 - 126
Byte n° 30 - 215
Byte n° 31 - 240
Byte n° 32 - 219
Byte n° 33 - 214
Byte n° 34 - 9
Byte n° 35 - 149
Byte n° 36 - 98
Byte n° 37 - 171
Byte n° 38 - 4
Byte n° 39 - 6
Byte n° 40 - 63
Byte n° 41 - 238
Byte n° 42 - 15
Byte n° 43 - 26
Byte n° 44 - 99
Byte n° 45 - 246
Byte n° 46 - 131
Byte n° 47 - 190
Byte n° 48 - 237
Byte n° 49 - 166
Byte n° 50 - 101
Byte n° 51 - 77
Byte n° 52 - 40
Byte n° 53 - 5
Byte n° 54 - 56
Byte n° 55 - 46
Byte n° 56 - 99
Byte n° 57 - 89
Byte n° 58 - 41
Byte n° 59 - 61
Byte n° 60 - 122
Byte n° 61 - 65
Byte n° 62 - 6
Byte n° 63 - 252
Byte n° 64 - 213
Byte n° 65 - 162
Byte n° 66 - 58
Byte n° 67 - 180
Byte n° 68 - 132
Byte n° 69 - 192
Byte n° 70 - 223
Byte n° 71 - 37
Byte n° 72 - 207
Byte n° 73 - 5
Byte n° 74 - 76
Byte n° 75 - 180
Byte n° 76 - 183
Byte n° 77 - 235
Byte n° 78 - 161
Byte n° 79 - 117
Device Name : )ْ©
'The device name is composed by 48 bytes, start from index 22 to index 69

Log VB.NET
B4X:
Byte n° 0 - 1
Byte n° 1 - 0
Byte n° 2 - 2
Byte n° 3 - 8
Byte n° 4 - 0
Byte n° 5 - 2
Byte n° 6 - 54
Byte n° 7 - 0
Byte n° 8 - 0
Byte n° 9 - 0
Byte n° 10 - 0
Byte n° 11 - 0
Byte n° 12 - 0
Byte n° 13 - 57
Byte n° 14 - 137
Byte n° 15 - 55
Byte n° 16 - 176
Byte n° 17 - 0
Byte n° 18 - 0
Byte n° 19 - 0
Byte n° 20 - 0
Byte n° 21 - 0
Byte n° 22 - 105
Byte n° 23 - 77
Byte n° 24 - 88
Byte n° 25 - 32
Byte n° 26 - 32
Byte n° 27 - 99
Byte n° 28 - 111
Byte n° 29 - 110
Byte n° 30 - 116
Byte n° 31 - 114
Byte n° 32 - 111
Byte n° 33 - 108
Byte n° 34 - 32
Byte n° 35 - 112
Byte n° 36 - 97
Byte n° 37 - 110
Byte n° 38 - 101
Byte n° 39 - 108
Byte n° 40 - 32
Byte n° 41 - 32
Byte n° 42 - 32
Byte n° 43 - 67
Byte n° 44 - 101
Byte n° 45 - 110
Byte n° 46 - 116
Byte n° 47 - 114
Byte n° 48 - 111
Byte n° 49 - 32
Byte n° 50 - 83
Byte n° 51 - 105
Byte n° 52 - 99
Byte n° 53 - 117
Byte n° 54 - 114
Byte n° 55 - 101
Byte n° 56 - 122
Byte n° 57 - 122
Byte n° 58 - 97
Byte n° 59 - 32
Byte n° 60 - 73
Byte n° 61 - 116
Byte n° 62 - 97
Byte n° 63 - 108
Byte n° 64 - 105
Byte n° 65 - 97
Byte n° 66 - 32
Byte n° 67 - 83
Byte n° 68 - 112
Byte n° 69 - 65
Byte n° 70 - 72
Byte n° 71 - 226
Byte n° 72 - 72
Byte n° 73 - 163
Byte n° 74 - 219
Byte n° 75 - 191
Byte n° 76 - 91
Byte n° 77 - 252
Byte n° 78 - 0
Byte n° 79 - 0
Byte n° 80 - 0
Device Name : iMX  control panel   Centro Sicurezza Italia SpA
 
Upvote 0

QtechLab

Active Member
Licensed User
Longtime User
I cannot give you the complete project because the class that manage the Socket and the AES Cipher is very big,
i've exported a little extract with the logic of that class.

If you want to compare with a vb.NET code, i used the Rijandel method.

My App will Dialog with a Security control panel, i cant give you the credentials.



Thanks in advance for youre help
 

Attachments

  • SocketCipher.zip
    1.7 KB · Views: 247
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Your code assumes that the data will arrive as a single message. This assumption is not correct.

The simplest way to solve all these issues is to switch to B4J. This will allow you to use AsyncStreams in prefix mode and to use B4XEncryption which works on all platforms.

Check the size of buffer. Make sure that it contains the correct data.
 
Upvote 0

QtechLab

Active Member
Licensed User
Longtime User
In my case, sometimes arrive multiple packets of 80 Bytes. Before know to do, the program has to decrypt the incoming packet. You say that in b4j there is B4X Encryption, how can i import that in B4I?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

QtechLab

Active Member
Licensed User
Longtime User
Assume that my network stream works fine (i've tried with PrefixMode but i don't receive anything).
And assume that i cannot change the encryption method (it is written in C++ in a microprocessor of a security control panel).
AES 128 - CBC - padding zeros

I have a VB.NET desktop application that decrypts perfectly with rijandel method
I have a B4I app that decrypts correctly only a little part of the buffer... I still have my decripting problem...

I cannot understand how Network streams can change the decryption result.

If you find helpful i upload the encryption library that use the securty control panel
 

Attachments

  • aes128 - Cpp.zip
    4.3 KB · Views: 246
Upvote 0

QtechLab

Active Member
Licensed User
Longtime User
I've verified that i receive all the bytes, i still have my firsts 14 bytes decrypted well and the others decrypted wrong.
Maybe is a bug in iEncryption library...

I've converted the C++ AES code in objective-C, can you take a look and add those files as library in Hosted builder?
 

Attachments

  • iPrivateAES.zip
    4.8 KB · Views: 223
Upvote 0
Top