iOS Question Error decoding data as string AES Decryption

Nana Osei Afrifa

Member
Licensed User
Please am getting this error when decrypting an encrypted string using IV and . "Error decoding data as string."
When I Use Dim result As String= BytesToString(data,0,data.Length,"UTF-8")
<B4IExceptionWrapper: Error Domain=caught_exception Code=0 " Error decoding data as string." UserInfo={NSLocalizedDescription= Error decoding data as string.}>


But When I Use Dim result As String= Bconv.HexFromBytes(data) , then there is no error
Please I want to get the original string but not in hexa format.
 

Nana Osei Afrifa

Member
Licensed User
Here's the encryption code I used.

B4X:
Sub Encrypt(Msg As String) As String
    Dim op As Int
    Dim Bconv As ByteConverter
    Dim data(0) As Byte
    Dim iv(0) As Byte
    Dim ivtxt As String="@" & Main.RandomString(15,True,True,True,"")
    iv = ivtxt.GetBytes("UTF8")
    data = Bconv.StringToBytes(Msg, "UTF8")
    op = Bit.Or(c.OPTION_ECBMode, c.OPTION_PKCS7Padding)
  
    data = c.Encrypt2(data, key,"AES",iv, op)
  
    Dim newarr(iv.Length + data.Length) As Byte
    Bconv.ArrayCopy(iv,0,newarr,0,iv.Length)
    Bconv.ArrayCopy(data,0,newarr,iv.Length,data.Length)
  
    Dim result As String
    result=su.EncodeBase64(newarr)
    Return result
End Sub
Here's the decryption code
B4X:
Sub Decrypt(EncMsg As String) As String
    Try
        Dim op As Int
        Dim Bconv As ByteConverter
        Dim fulldata(0) As Byte
        fulldata = su.DecodeBase64(EncMsg)
       
        Dim ln As Int
        ln=fulldata.Length-16
        Dim data(ln) As Byte
        Dim iv(16) As Byte
       
        Bconv.ArrayCopy(fulldata,0,iv,0,16)
        Bconv.ArrayCopy(fulldata,16,data,0,ln)
       
        op = Bit.Or(c.OPTION_ECBMode, c.OPTION_PKCS7Padding)
       
        data = c.Decrypt2(data, key,"AES",iv,op)
        Dim result As String= BytesToString(data,0,data.Length,"UTF-8")' Bconv.HexFromBytes(data)
       
        Return result
    Catch
        Log(LastException)
    End Try
    Return ""
End Sub
 

Nana Osei Afrifa

Member
Licensed User
Here's the

Data before decryption
862ED1E12C173E3921B1F7EE63EE20B470B02ABF75CD80A404CA739049269D7157EA4526D33888C4972AFCE165E669757141B073561E83D240E519D1465483FA7E0EBECD34863D39D113AFCFC2791622FE6C14FAA86E4E536A2800506DDB79AD
Data after decryption
3E12B1AD869FB49D7DC1E0ACAAA5551D484590570D08F6EDA2302086E4338AE211A184F0ACC8F3B99C0C07C43106C5C60540EDF87198D22CCA7F22A245F9C9D82C022482F79D90A2EBCA067604A020026F75AF10E299ED3653D4FAAEB4546A2C

The encrypted data from server is encryped using cipher option PKCS5Padding, but there's no PKCS5Padding option on the cipher library of b4i. How do i specify the cipher option like I did in android.

B4X:
'Android version of the cipher
Dim c As Cipher
c.Initialize("AES/CBC/PKCS5Padding")
c.InitialisationVector = iv
.
 

Nana Osei Afrifa

Member
Licensed User
Please what about the padding because our server is using PKCS5Padding to encrypt but there's no PKCS5Padding in B4i cipher library option.
 

Nana Osei Afrifa

Member
Licensed User
Am still not able to solve this problem. I used UTF-8 encoding between my android application with the web services and everything works perfect.
But always getting error when I use B4I with UTF-8. Please help.
 
Top