Android Question EncodeBase64 for URL and Files

ALBRECHT

Active Member
Licensed User
Hello,

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

B4X:
Sub EncryptText(text As String, password As String) As 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 String) As 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:

emexes

Expert
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
 
Upvote 0

ALBRECHT

Active Member
Licensed User
Hello,

im using :
B4X:
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 ?
 
Upvote 0

ALBRECHT

Active Member
Licensed User
an example of return : (with an /)

AppUserMpass : LButHLwoaq8jOd92jzY76oavNIbLwVguoY/qjYPkgRgfO7FLEzSAcw==
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime 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:
Upvote 0

emexes

Expert
Licensed User
Return su.EncodeBase64(c.Encrypt(text.GetBytes("utf8"), password))
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:
Upvote 0

ALBRECHT

Active Member
Licensed User
Ok

I send only small settings strings with PostString :

B4X:
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
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I send only small settings strings with PostString
You are NOT POSTing anything than an empty String.
B4X:
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

B4X:
    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:
Upvote 0

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:
Upvote 0
Top