iOS Question [SOLVED] B4XEncryption to iEncryption: Error decoding data as string.

yiankos1

Well-Known Member
Licensed User
Longtime User
Hello,

I think it is the last post about porting b4a to b4i. I use this b4a code in order to encrypt and decrypt chat messages:
B4X:
Sub EncryptText(text As String, password As String) As Byte()
    #if b4a
    Dim c As B4XCipher
    #Else
    Dim c As Cipher
    #End If
    Return c.Encrypt(text.GetBytes("utf8"), password)
End Sub

Sub DecryptText(EncryptedData() As Byte, password As String) As String
    #if b4a
    Dim c As B4XCipher
    #Else
    Dim c As Cipher
    #End If
    Dim b() As Byte = c.Decrypt(EncryptedData, password)
    Return BytesToString(b, 0, b.Length, "UTF8")
End Sub

In b4i throws this error:
B4X:
*** fullprofilepage: B4XPage_Disappear [mainpage, fullprofilepage, chatpage]
*** chatpage: B4XPage_Appear [mainpage, fullprofilepage, chatpage]
*** chatpage: B4XPage_Resize [mainpage, fullprofilepage, chatpage]
ready
Class (b4i_dbrequestmanager) instance released.
Class (b4i_httpjob) instance released.
Error occurred on line: 141 (misc)
Error decoding data as string.
Stack Trace: (
  CoreFoundation       309E77AA-4FF2-35B9-8AF6-33AF9C2B7929 + 601236
  libobjc.A.dylib      objc_exception_throw + 56
  CoreFoundation       309E77AA-4FF2-35B9-8AF6-33AF9C2B7929 + 941700
  fitbook.gr           -[B4ICommon BytesToString::::] + 264
  CoreFoundation       309E77AA-4FF2-35B9-8AF6-33AF9C2B7929 + 134416
  CoreFoundation       309E77AA-4FF2-35B9-8AF6-33AF9C2B7929 + 249336
  fitbook.gr           +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 1348
  fitbook.gr           -[B4IShell runMethod:] + 420
  fitbook.gr           -[B4IShell raiseEventImpl:method:args::] + 1472
  fitbook.gr           -[B4IShellBI raiseEvent:event:params:] + 1360
 fitbook.gr           -[b4i_misc _decrypttext::] + 316
 fitbook.gr           -[ResumableSub_chat_getChatMessages resume::] + 4164
 CoreFoundation       309E77AA-4FF2-35B9-8AF6-33AF9C2B7929 + 134416
 CoreFoundation       309E77AA-4FF2-35B9-8AF6-33AF9C2B7929 + 249336
 fitbook.gr           +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 1348
 fitbook.gr           -[B4IShell runMethod:] + 420
 fitbook.gr           -[B4IShell raiseEventImpl:method:args::] + 1968
 fitbook.gr           -[B4IShellBI raiseEvent:event:params:] + 1360
 fitbook.gr           -[B4IDelegatableResumableSub resume::] + 376
 fitbook.gr           -[B4I checkAndRunWaitForEvent:event:params:] + 468
 fitbook.gr           -[B4IShellBI raiseEvent:event:params:] + 1216
 fitbook.gr           __33-[B4I raiseUIEvent:event:params:]_block_invoke + 52
 libdispatch.dylib    34EF3925-0303-30BF-9946-AD99311620DD + 405652
 libdispatch.dylib    34EF3925-0303-30BF-9946-AD99311620DD + 409748
 libdispatch.dylib    34EF3925-0303-30BF-9946-AD99311620DD + 68932
 libdispatch.dylib    34EF3925-0303-30BF-9946-AD99311620DD + 67988
 CoreFoundation       309E77AA-4FF2-35B9-8AF6-33AF9C2B7929 + 319540
 CoreFoundation       309E77AA-4FF2-35B9-8AF6-33AF9C2B7929 + 46392
 CoreFoundation       CFRunLoopRunSpecific + 572
 GraphicsServices     GSEventRunModal + 160
 UIKitCore            697C7D5C-9761-36E9-8E0F-200035BF3F39 + 5134984
 UIKitCore            UIApplicationMain + 336
 fitbook.gr           main + 100
 dyld                 start + 444
)

As I read at posts there is a problem about charset. Here is table structure for encrypted chat messages:


Any ideas?

Thank you for your time.
 
Solution
If someone else have the same problem, when storing encrypted data in SQL and in order to be multi-platform, use this code:
B4X:
Sub EncryptText(text As String, password As String) As String
    #if b4a
    Dim c As B4XCipher
    #Else
    Dim c As Cipher
    #End If
    Private su As StringUtils
    Private b() As Byte = c.Encrypt(text.GetBytes("utf8"), password)
    Return su.EncodeBase64(b)
End Sub

Sub DecryptText(EncryptedData As String, password As String) As String
    #if b4a
    Dim c As B4XCipher
    #Else
    Dim c As Cipher   
    #End If
    Private su As StringUtils
    Private base() As Byte = su.DecodeBase64(EncryptedData)
    Dim b() As Byte = c.Decrypt(base, password)
    Return BytesToString(b, 0, b.Length, "UTF8")
End...

yiankos1

Well-Known Member
Licensed User
Longtime User
Can you reproduce it in a small project? Without the database. Pass the string directly.
Hello Erel,

Thanks for the answer. I can provide a small project, but what do you mean "string"? When I query chat message, it is in byte format stored in blob cell. It is not plain text. This error occures when quering this encrypted data.

So, how whould you like the demo project in order to reproduce the error?

Thank you for your help.
 
Upvote 0

yiankos1

Well-Known Member
Licensed User
Longtime User
1. I haven't understood exactly what you are doing, however make sure not to try to convert raw bytes to string. It will result with the error that you are seeing above.
When a user send a chat message, i encrypt the text with this code:
B4X:
Sub EncryptText(text As String, password As String) As Byte()
    #if b4a
    Dim c As B4XCipher
    #Else
    Dim c As Cipher
    #End If
    Return c.Encrypt(text.GetBytes("utf8"), password)
End Sub

And then i store these returning bytes at SQL DB in a blob type cell. When i recieve these bytes from db(in order to decrypt), in b4i throws OP's exception but in b4a everything working fine.

Do you mean that i have to do something in these ecrypted bytes before uploading in DB? Do i have to transform to something else?

Thank you for assistance.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Full example:
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    Dim password As String = "123456"
    Dim text As String = "top secret"
    Dim b() As Byte = EncryptText(text, password)
    Log(DecryptText(b, password))
End Sub


Sub EncryptText(text As String, password As String) As Byte()
    #if b4a
    Dim c As B4XCipher
    #Else
    Dim c As Cipher
    #End If
    Return c.Encrypt(text.GetBytes("utf8"), password)
End Sub

Sub DecryptText(EncryptedData() As Byte, password As String) As String
    #if b4a
    Dim c As B4XCipher
    #Else
    Dim c As Cipher
    #End If
    Dim b() As Byte = c.Decrypt(EncryptedData, password)
    Return BytesToString(b, 0, b.Length, "UTF8")
End Sub

This works.
 
Upvote 0

yiankos1

Well-Known Member
Licensed User
Longtime User
If someone else have the same problem, when storing encrypted data in SQL and in order to be multi-platform, use this code:
B4X:
Sub EncryptText(text As String, password As String) As String
    #if b4a
    Dim c As B4XCipher
    #Else
    Dim c As Cipher
    #End If
    Private su As StringUtils
    Private b() As Byte = c.Encrypt(text.GetBytes("utf8"), password)
    Return su.EncodeBase64(b)
End Sub

Sub DecryptText(EncryptedData As String, password As String) As String
    #if b4a
    Dim c As B4XCipher
    #Else
    Dim c As Cipher   
    #End If
    Private su As StringUtils
    Private base() As Byte = su.DecodeBase64(EncryptedData)
    Dim b() As Byte = c.Decrypt(base, password)
    Return BytesToString(b, 0, b.Length, "UTF8")
End Sub

Moreover, column type that stores encrypted data will be TEXT:


Using Base64 encoding will fix OP exception in B4i.

In B4A both codes work properly (OP and this one).
 
Upvote 0
Solution
Cookies are required to use this site. You must accept them to continue using the site. Learn more…