iOS Question Error decoding data as string AES Decryption

Discussion in 'iOS Questions' started by Nana Osei Afrifa, Feb 6, 2018.

  1. Nana Osei Afrifa

    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.
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Moved to B4i forum.

    How did you encrypt the string? Maybe you have used a different encoding.
     
  3. Nana Osei Afrifa

    Nana Osei Afrifa Member Licensed User

    Here's the encryption code I used.

    Code:
    Sub Encrypt(Msg As StringAs String
        
    Dim op As Int
        
    Dim Bconv As ByteConverter
        
    Dim data(0As Byte
        
    Dim iv(0As 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
    Code:
    Sub Decrypt(EncMsg As StringAs String
        
    Try
            
    Dim op As Int
            
    Dim Bconv As ByteConverter
            
    Dim fulldata(0As Byte
            fulldata = su.DecodeBase64(EncMsg)
           
            
    Dim ln As Int
            ln=fulldata.Length-
    16
            
    Dim data(ln) As Byte
            
    Dim iv(16As 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 StringBytesToString(data,0,data.Length,"UTF-8")' Bconv.HexFromBytes(data)
           
            
    Return result
        
    Catch
            
    Log(LastException)
        
    End Try
        
    Return ""
    End Sub
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    Tip:
    Instead of:
    Code:
    Dim fulldata(0As Byte
            fulldata = su.DecodeBase64(EncMsg)
    Write:
    Code:
    Dim fulldata() As Byte = su.DecodeBase64(EncMsg)
    What is the output of:
    Code:
    Log(Bconv.HexFromBytes(data)) 'right after you convert Msg to an array of bytes
    Log(Bconv.HexFromBytes(data)) 'right after you decrypt the data
     
  5. Nana Osei Afrifa

    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.

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

    Erel Administrator Staff Member Licensed User

    You shouldn't set ECBMode. The default mode is CBC.
     
  7. Nana Osei Afrifa

    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.
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    I don't think that this is the cause of this error. You should be able to use PKCS7Padding instead.
     
  9. Nana Osei Afrifa

    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.
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    Can you upload two small examples, one with B4A and one with B4i, with your encryption code?
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice