iOS Question Encryption/Decryption Problem

Pablo Torres

Active Member
Licensed User
Longtime User
Hi all,

I have a B4J Server that runs an encrypt/decrypt routine while send/receive data

Also, I have a B4i App that do exactly the same and comunicate with the B4j server.

The Encrypt/Decrypt routines for B4J are:
B4X:
Public Sub EncryptText(text As String) As String
    Dim c As B4XCipher
    Dim su As StringUtils
    Return su.EncodeBase64(c.Encrypt(text.GetBytes("utf8"), "Manolito10"))
End Sub

Public Sub DecryptText(EncryptedData As String) As String
    Dim c As B4XCipher
    Dim su As StringUtils
    Dim b() As Byte = c.Decrypt(su.DecodeBase64(EncryptedData), "Manolito10")
    Return BytesToString(b, 0, b.Length, "utf8")
End Sub

The Encrypt/Decrypt routines for B4i are:
B4X:
Public Sub EncryptText(text As String) As String
    Dim c As Cipher
    Dim su As StringUtils
    Return su.EncodeBase64(c.Encrypt(text.GetBytes("utf8"), "Manolito10"))
End Sub

Public Sub DecryptText(EncryptedData As String) As String
    Dim c As Cipher
    Dim su As StringUtils
    Dim b() As Byte = c.Decrypt(su.DecodeBase64(EncryptedData), "Manolito10")
    Return BytesToString(b, 0, b.Length, "utf8")
End Sub

If I take out the Encrypt/Decrypt routines everything works great but including those routines it does not work

Can anyone help me? please
Many thanks to take the time to read my thread
 

emexes

Expert
Licensed User
Sounds like the same problem as thread MQTT adds extra characters when encrypted.

Cause and workaround in post #13 of same thread.

Even-simpler fix in post #15 of same thread.

Applied to your code, this would involve a one-line change to your B4I DecryptText function:
B4X:
Return BytesToString(b, 0, b.Length, "utf8").Replace(Chr(0), "")
although there is no harm in adding it the B4J code as well, if you want to maximize your sanity by minimizing your code differences between various platforms ;-)
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
@emexes : someone needs to post a bug report so a proper fix can be done. As pointed out by @Sandman in the other thread, the given solution may introduce other subtle bugs. We need a real fix, not a workaround
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Thank you. Tip: use ByteConverter to examine raw bytes:

B4X:
Dim msg As String = "3670BB9F//16RR22200732409191110069"
Log(msg.Length)
Dim c As Cipher
Dim encryptedBytes() As Byte = c.Encrypt(msg.GetBytes("ascii"), "SecurePassword")
Dim dec() As Byte = c.Decrypt(encryptedBytes, "SecurePassword")
Log(dec.Length)
Log(BytesToString(dec, 0, dec.Length, "ascii"))
Dim bc As ByteConverter
Log(bc.HexFromBytes(dec))

This issue is fixed in iEncryption v1.03.
 
Upvote 0
Top