Page 1 of 4

base64encode/DB replacing all "+" symbols with "space" ?

Posted: Sat Feb 03, 2018 2:08 am
by teriibi
Hi,
I´m trying to compare two values generated using Hash and Base64encode, one local, the other retreived from a Sql Db.
In the one I retreive from DB, the plus symbol "+" is replaced by a "space". all other caracters of the string remains the same.
I googled and find various comments mentioning the same issue... :roll:

Can anyone tell me:
!) if they have also faced this issue in the past ? and
2) how to possibly get arround the problem - in LC - with a quick solution (other encoding maybe, change DB field format... :?: :?: :?:
(...without having to learn another coding language :!: :!: :lol:)

Here are the 2 strings, I store each in 2 fields before I start to compare them...
String 1

Code: Select all

k/DATXDPBQnk9efENHIc2+cWIPKFrzuObg3azWYthAUqUPbpelBdzs0VGno6M+yPc8D5+S3Y
eTnq9QaunubWJA==
String 2 (retreived from DB)

Code: Select all

k/DATXDPBQnk9efENHIc2 cWIPKFrzuObg3azWYthAUqUPbpelBdzs0VGno6M yPc8D5 S3Y
eTnq9QaunubWJA==
...All 3 "+" signs where replaced :!: :?: :?:

LC Comparing Code:

Code: Select all

on mouseup
   if fld "res1" is fld "res2" then
      put "Same string" into fld "info"
   else
      put "different strings" into fld "info"
   end if
end mouseup
REFERED SIMILAR ISSUE IN STACKOVERFLOW
I have a test entry in the database whose description is:
AAA `~!@#$%^&*()_+-={}|[]\:";'<>?,./ And wow this has a lot of special characters.
With the code above, for some reason when the request gets to the controller, the description loses the + sign (it becomes just a space).

Base64encode unwanted character-switch : "+"<>"space" !

Posted: Sat Feb 03, 2018 2:23 pm
by teriibi
Hi,
I dont know - yet - if the base64Encode problem I am facing now is:
- a bug that LC can solve..or
- a bug from a third party software implied
- one from a Database limitation
...or just a missconfiguration from my script.
*viewtopic.php?f=11&t=30563

You can see the problem described above :
it was also related on StackOverflow* - but i could not understand how to apply a solution..if any yet.
(if its a bug from LC ...the above Topic could be moved here)

Script Code used to create the Hash locally:

Code: Select all

   ## Creating the Hash from tinput
   put messageDigest(tinput, "SHA3-512") into tOHash
   ## encoding to 64Encode
   put base64Encode(tOHash into datahashed
   put datahashed into fld "stringA"
   
   ## String hash is sent this way
    put "datahashed="&datahashed into Outgoing
   post Outgoing to url"http://www.myserver.com/addhash.php"
   ...
   
- Exact same code is used to create the 2nd HASH that is sent and stored on the MySql DB.
- DB field Format to store Hash uses VARCHAR (150)

Code: Select all

   ## Retreiving the previously stored Hash from the DB
   ## RecID is related to record´s ID
   post RecID to url "http://www.myserver.com/hash.php"
   put it into toParse
   ## Store received Hash into fiel for comparing Op. - String is NOT Decoded since its not needed.
   put item 1 of toParse into field "stringB"
   
   if fld "stringA" is "stringB" then
      put "Hashes are the same" into fld "info"
      ...
      
Strings displays fine in fileds but retreived String B is different from A since it now lacks ALL "+" signs originaly present.
Any real helping tips is welcomed. :?: :shock:

Re: base64encode/DB replacing all "+" symbols with "space" ?

Posted: Sat Feb 03, 2018 7:09 pm
by FourthWorld
1. I've never seen a base64 string that included spaces. Could the database have a bug?

2. Why base64 a hash? Hex is a more commonly used representation.

Re: base64encode/DB replacing all "+" symbols with "space" ?

Posted: Sat Feb 03, 2018 7:17 pm
by jacque
It sounds like the db thinks the string is url encoded and is translating it before sending it back. There may be a way to tell the db not to do that. Or in the LC script, just do this:

replace space with "+" in tVar -- use your variable name

Re: Base64encode unwanted character-switch : "+"<>"space" !

Posted: Sat Feb 03, 2018 10:36 pm
by MaxV
The problem is you have to urlencode before sending by internet, otherwise special chars like + will transformed.
The urlencode change chars in safe transfer way.
Then you go back with urldecode.

For example:
urlencode("Ciao+") = "ciao%2B"

Re: Base64encode unwanted character-switch : "+"<>"space" !

Posted: Sun Feb 04, 2018 12:43 pm
by teriibi
Oh, ok, my fault then...I ll try to fix that asap. :oops:
Tks

Re: base64encode/DB replacing all "+" symbols with "space" ?

Posted: Sun Feb 04, 2018 5:05 pm
by teriibi
Hello ppl,

1rst things first.
someone is telling me that the script misses adding of the URLencode (which I am about to do now)

2nd...
since I thought it "could" be a bug, I then opened a Topic in the "possible bug" :
viewtopic.php?f=104&t=30567
This topic shows the script I used...

3rd : I m very new to this so..
to the question why Base64. ?
if i don t do it, I get all funny ???c??? character string that are stored in the DB field.
Using base64...everything display fine...just as it displays when I retreive each Value.

Could the DB have a Bug...dont know yet. I will first encodeURL and see if it keeps removing all "+"...
:wink:

Re: base64encode/DB replacing all "+" symbols with "space" ?

Posted: Mon Feb 05, 2018 2:47 am
by mwieder
I see you have a response in the bugreport thread about urlencoding the string before posting it. That should solve the problem. The issue here is not with LiveCode or with the database or with base64 encoding itself, but with passing around information on the internet, where there are special characters that have a specific meaning there. Urlencoding the string before sending it to a remote database (and urldecoding it when you get it back) should take care of this. If not, post details here and we'll figure something else out.

Re: base64encode/DB replacing all "+" symbols with "space" ?

Posted: Mon Feb 05, 2018 2:09 pm
by teriibi
Negativ news - applying URLencode / Urldecode steps doesnt solve the missing "+" :shock:
see steps taken in "Bug Post section please"

Re: Base64encode unwanted character-switch : "+"<>"space" !

Posted: Mon Feb 05, 2018 2:13 pm
by teriibi
So, after applying the URlEncode and Decode steps, Im still gettin missing "+" replaced by empty spaces.

Would this have to do with the Database field ? I can´t think of another factor that could affect a value.
I dont think transfering converting data over the internet would "loose" a caracter in the process for this would have been noticed long time ago. meanwhile..using an unusual format combination for the DB "could" result in an unexpected bug...or am i going the wrong way here ? :shock:
DB field Format CHAR(128) UTF8_unicode_ci, also tested with Bynary and UTF16...
:!:

Modified script:

Code: Select all

   put messageDigest(tinput, "SHA3-512") into tOHash
   ## encoding to 64Encode
   put base64Encode(tOHash) into datahashed
    ## encoding to URLEncode
   put URLencode(datahashed) into dataU
   
   put datahashed into fld "stringA" 
   ##(not using the URL encoded value for comparision)
   
   ## String hash is sent this way
    put "datahash="&dataU into Outgoing
   post Outgoing to url"http://www.myserver.com/addhash.php"

Re: Base64encode unwanted character-switch : "+"<>"space" !

Posted: Mon Feb 05, 2018 5:59 pm
by dave.kilroy
Are you urlDecodeing in your php script on the server? If so then try without doing this step (your server should urldecode it without you doing anything)

Re: base64encode/DB replacing all "+" symbols with "space" ?

Posted: Mon Feb 05, 2018 6:07 pm
by FourthWorld
Threads merged.

The binary values returned from good hash functions are most commonly transmitted and stored as hexadecimal. LC's binaryConvert function can do this:

Code: Select all

function CleanHash pInput
   put messageDigest(pInput, "SHA3-512") into tOHash
   get binaryDecode("H*", tOHash, tHex)
   return tHex
end CleanHash

Re: base64encode/DB replacing all "+" symbols with "space" ?

Posted: Mon Feb 05, 2018 8:22 pm
by teriibi
to Dave.
Nop, I m not doing anything on the server side. Php links the values to the DB, thats all.
The only encoding op..is made on the Client side from values it sends..
Then when needing to retreive decodes and compare...
i was sending it at first without Urlencoding..then used the Urlencode...which didnt make a diference.
in both case, "+" signs are replaced by a "space"...

Re: base64encode/DB replacing all "+" symbols with "space" ?

Posted: Mon Feb 05, 2018 8:27 pm
by teriibi
To fourthWorld,
Are you suggesting that I should stop using base64encode and just replace it with:

binaryDecode("H*", tOHash, tHex)
...at uploading time ?
at both - uploading & downloading.?
..or just downloading time ?
:roll:

Re: base64encode/DB replacing all "+" symbols with "space" ?

Posted: Mon Feb 05, 2018 8:30 pm
by dave.kilroy
OK good (that you're not urldecoding on the server). I'm still suspicious that too many urlDecode() calls are being made because urldecode("secretText+") will produce "secretText ". When you retrieve text back from the db what does it look like before you urldecode() it in your app?