1. *** New version of B4J is available ***
    B4J v7.8
    Dismiss Notice

Android Question EncodeBase64 for URL and Files

Discussion in 'Android Questions' started by ALBRECHT, Aug 4, 2019.

  1. ALBRECHT

    ALBRECHT Active Member Licensed User

    Hello,

    Im using that kind of sub with StringUtils and B4XCipher library to encode some passwords :

    Code:
    Sub EncryptText(text As String, password As StringAs String
        
    Dim c As B4XCipher
        
    Dim su As StringUtils
        
    Return su.EncodeBase64(c.Encrypt(text.GetBytes("utf8"), password))
    End Sub

    Sub DecryptText(EncryptedData As String, password As StringAs String
        
    Dim c As B4XCipher
        
    Dim su As StringUtils
        
    Dim b() As Byte = c.Decrypt(su.DecodeBase64(EncryptedData), password)
        
    Return BytesToString(b, 0, b.Length, "utf8")
    End Sub
    but for URL, if these chars / + = are eventually returned, which could generate some pbs when passing in URL,

    - If yes, should i replace by myself that chars (by replacing character 62 and 63 with - and _ respectively)
    - or is there an EncodeBase64URL methode
    - or otherwise

    Regard
    Michel
     
    Last edited: Aug 4, 2019
  2. Erel

    Erel Administrator Staff Member Licensed User

    It depends on the http request type and encoding. Simplest way is to send POST requests and read the raw payload.

    If you need to send a GET request then use HttpJob.Download2. It will url encode the parameters.
     
  3. emexes

    emexes Well-Known Member Licensed User

    Erel beat me to the punch, but I was 75% done and perhaps there's something useful here anyway, so:

    Encode64 needs 64 distinct characters to encode 6 bits. 26 uppercase letters and 26 lower case letters and 10 digits = 62 characters, so two extra characters are needed. The B4X EncodeBase64 uses the MIME choice of "+" and "/", and "=" for padding at the end if there is not a whole number of 3 byte groups.

    All three of those characters should be safe for use at the end of a URL, after the "?" here-come-the-parameters marker.

    If you wanted to be sure, you could replace the characters with something else after encoding, and ditto in reverse before decoding.

    Or there is this in StringUtils, maybe that's something to look at:

    upload_2019-8-4_17-29-41.png

    Or you could write your own EncodeBase52 that just uses letters, or EncodeBase32, or EncodeBase16... no, wait, that last one's already done:

    upload_2019-8-4_17-32-53.png
     
    Erel likes this.
  4. ALBRECHT

    ALBRECHT Active Member Licensed User

    Hello,

    im using :
    Code:
    job.PostString(BoServerAddUserUrl & "?" & PSParams, "")
    (with me, the second setting for charge dont works, so i send like that )

    So i have to make an encodeURL with all my PostSring charge values that i pass inside PSParams ?
     
  5. ALBRECHT

    ALBRECHT Active Member Licensed User

    an example of return : (with an /)

    AppUserMpass : LButHLwoaq8jOd92jzY76oavNIbLwVguoY/qjYPkgRgfO7FLEzSAcw==
     
  6. DonManfred

    DonManfred Expert Licensed User

    Note that the length of a url is limited. In your case you are all sending as get parameter which ends in a long url.
    If you, for example upload a image (base64 encoded) you´ll most probably reach the maximum allowed url-length.

    Check Google.
     
    Last edited: Aug 4, 2019
    Jorge M A and emexes like this.
  7. emexes

    emexes Well-Known Member Licensed User

    I was shying away from using hexadecimal because it is easily recognisable and thus less obfuscative, but if you're actually doing real encryption, then disguising the encrypted data is less of a concern. If you're only talking small quantities of data, eg < few kBs on disk or < few hundred bytes over network, then HexFromBytes is a reasonable choice.

    Just remember that it increases your data size by another 50% compared to Base64. And that disk/network space is cheap compared to your time ;-)
     
    Last edited: Aug 4, 2019
  8. ALBRECHT

    ALBRECHT Active Member Licensed User

    Ok

    I send only small settings strings with PostString :

    Code:
    job.PostString(BoServerAddUserUrl & "?" & PSParams, "")
    so I will opt for the encodeURL of my charge values : PSParams
    (that will automaticaly replace the 62 + 63 code inside the base64 encoded param)

    Thanks for all
     
  9. DonManfred

    DonManfred Expert Licensed User

    You are NOT POSTing anything than an empty String.
    Code:
    job.PostString(BoServerAddUserUrl & "?" & PSParams, "THIS IS THE STRING WHICH IS POSTED"' PSParams are giving as GET values
    I suggest to use PostMultipart where the Map should contain all your Values (PSParams) and an null for the Filelist.
    You don´t need to urlencode anything

    Code:
    Dim job As HttpJob
        job.Initialize(
    "",Me)
        job.Initialize(
    "post job",Me)
      
        
    Dim PSParams As Map
        PSParams.Initialize
        PSParams.Put(
    "Username","myusername"' these values are given by POST in the PostMultipart-Call
        PSParams.Put("Other","otherstring"' dito
        PSParams.Put("Image","base64 image string here"' base64 String. NOT needed to urlencode

        job.PostMultipart(BoServerAddUserUrl,PSParams,
    Null)
     
    Last edited: Aug 4, 2019
  10. ALBRECHT

    ALBRECHT Active Member Licensed User

    Ok i will try PostMultipart (and switch with post method)
    because my values already come from a map

    Thanks
     
    Last edited: Aug 4, 2019
    emexes likes this.
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