iOS Question Problem to crypt simple strings

schimanski

Well-Known Member
Licensed User
Longtime User
I have some problems to encrypt simple strings. The problem is, that an emty string ("") after encrypt and decrypt isn't longer an emty string. It's an array of bytes with a length of 16. How is it possible to get the emty string after decryption?

B4X:
Sub Encrypt(Zeichenkette As String) As Byte()
    Dim ByteArray() As Byte
    ByteArray=Cipher1.Encrypt(Zeichenkette.GetBytes("UTF8"), "xxxxx")
    Return ByteArray
End Sub

Sub Decrypt(Zeichenkette() As Byte) As String
    Dim ByteArray() As Byte
    ByteArray=Cipher1.Decrypt(Zeichenkette, "xxxxx")
    Return BytesToString(ByteArray, 0, ByteArray.Length, "UTF8")
End Sub
 

schimanski

Well-Known Member
Licensed User
Longtime User
I have made more tests and my problem is not only an emty string. If I encrypt for example one string ("test"), and show the result after decryption in the log, the text is "test". But it is not the same because if I check the result with if result="test", then i get a false:(. The result of my Decrypt-sub is "test" with an length from 16????
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
The returned string will contain additional chr(0)'s (I think) at the end to the length of the byte array.

Try something like:

B4X:
Dim Str As String  = BytesToString(ByteArray, 0, ByteArray.Length, "UTF8")
Return Str.SubString2(0,Str.IndexOf(Chr(0))
 
Upvote 0
Top