Encrypting html data received from ajax request? - php

How can I encrypt the data receieved from an ajax request? For example I noticed from Google when a get request is made the data that is receieved is all scambled like :
position:absolute;top:0;width:155px\\x22 onclick\\x3d\\x22google.psy\\x26\\x26google.psy.qs(event)\\x22\\x3e\\x3cdiv id\\x3dms\\x3e\\x3cul\\x3e\\x3cli class\\x3d\\x22mitem msel\\x22\\x3e\\x3cspan class\\x3dmicon style\\x3d\\x22background-position:-20px -85px\\x22\\x3e\\x3c/span\\x3eEverything\\x3cli class\\x3d\\x22mitem\\x22\\x3e\\x3ca href\\x3d\\x22/search?q\\x3dasdfadf\\x26amp;hl\\x3den\\x26amp;prmd\\x3divns\\x26amp;source\\x3dlnms\\x26amp;tbm\\x3disch\\x26amp;ei\\x3dHZy4TeKYG6bw0gG8pbTRDw\\x26amp;sa\\x3dX\\x26amp;oi\\x3dmode_link\\x26amp;ct\\x3dmode\\x26amp;cd\\x3d2\\x26amp;sqi\\x3d2\\x26amp;ved\\x3d0CAgQ_AUoAQ\\x22 class\\x3d\\x22q qs\\x22\\x3e\\x3cspan class\\x3dmicon style\\x3d\\x22background-position:-40px -85px\\x22\\x3e\\x3c/span\\x3eImages\\x3c/a\\x3e\\x3cli class\\x3d\\x22mitem\\x22\\x3e\\x3ca href\\x3d\\x22/search?q\\x3dasdfadf\\x26amp;hl\\x3den\\x26amp;prmd\\x3divns\\x26amp;source\\x3dlnms\\x26amp;tbm\\x3dvid\\x26amp;ei\\x3dHZy4TeKYG6bw0gG8pbTRDw\\x26amp;sa\\x3dX\\x26amp;oi\\x3dmode_link\\x26amp;ct\\x3dmode\\x26amp;cd\\x3d3\\x26amp;sqi\\x3d2\\x26amp;ved\\x3d0CAkQ_AUoAg\\x22 class\\x3d\\x22q qs\\x22\\x3e\\x3cspan class\\x3dmicon style\\x3d\\x22background-position:-80px -85px\\x22\\x3e\\x3c/span\\x3eVideos\\x3c/a\\x3e\\x3cli class\\x3d\\x22mitem\\x22\\x3e\\x3ca href\\x3d\\x22/search?q\\x3dasdfadf\\x26amp;hl\\x3den\\x26amp;prmd\\x3divns\\x26amp;source\\x3dlnms\\x26amp;tbm\\x3dnws\\x26amp;ei\\x3dHZy4TeKYG6bw0gG8pbTRDw\\x26amp;sa\\x3dX\\x26amp;oi\\x3dmode_link\\x26amp;ct\\x3dmode\\x26amp;cd\\x3d4\\x26amp;sqi\\x3d2\\x26amp;ved\\x3d0CAoQ_AUoAw\\x22 class\\x3d\\x22q qs\\x22\\x3e\\x3cspan class\\x3dmicon style\\x3d\\x22background-position:-120px -85px\\x22\\x3e\\x3c/span\\x3eNews\\x3c/a\\x3e\\x3cli class\\x3d\\x22mitem\\x22\\x3e\\x3ca href\\x3d\\x22/search?q\\x3dasdfadf\\x26amp;hl\\x3den\\x26amp;prmd\\x3divns\\x26amp;source\\x3dlnms\\x26amp;tbm\\x3dshop\\x26amp;ei\\x3dHZy4TeKYG6bw0gG8pbTRDw\\x26amp;sa\\x3dX\\x26amp;oi\\x3dmode_link\\x26amp;ct\\x3dmode\\x26amp;cd\\x3d5\\x26amp;sqi\\x3d2\\x26amp;ved\\x3d0CAsQ_AUoBA\\x22 class\\x3d\\x22q qs\\x22\\x3e\\x3cspan class\\x3dmicon style\\x3d\\x22background-position:-120px -105px\\x22\\x3e\\
How can I achieve a similar structure?

you can't.
in the code you posted it's not encryption but just encoding.

It is not really "encrypted" simply obfuscated and encoded. I am unsure of the encoding type being used. Check this SO post, which has a similar question and it states ASCII that is encoded hexadecimal.

The data example isn't scrambled, it's just encoded. Any 'strange' character is replace by \\x## where ## is some hexadecimal number representing that character.
\\x22 is probably a "
\\x3d is probably a =
and so on ..

Related

String to Integer convertion - identical in PHP and Javascript

I have a URL dynamically displayed with a PHP script. This URL comes to be the name of a CSS class. I need to use this class name into a jQuery script after an Ajax call response (All the HTML into this specific class has to be hidden).
The URL contains / and . and : — To make it easier in my jQuery script, I would like to convert the URL into an Integer with a PHP function (like hash("md5",)) ... and in my JavaScript, convert the URL again into an integer that will be obviously the same.
I read that How to calculate md5 hash of a file using javascript
but it doesn't look like the best solution. Does anyone have a more intelligent solution?
Regards
You are probably looking for encoding and not hashing, as you want to read the URL back. I'd try using base64 - on the server side: http://php.net/manual/en/function.base64-decode.php and http://php.net/manual/en/function.base64-encode.php
And on client (JavaScript) side: http://www.webtoolkit.info/javascript-base64.html, How can you encode a string to Base64 in JavaScript?

UTF-8 data received by php isn't decoded

I'm having some troubles with my $_POST/$_REQUEST datas, they appear to be utf8_encoded still.
I am sending conventional ajax post requests, in these conditions:
oXhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
js file saved under utf8-nobom format
meta-tags in html <header> tag setup
php files saved under utf-8-nobom format as well
encodeURIComponent is used but I tried without and it gives the same result
Ok, so everything is fine: the database is also in utf8, and receives it this way, pages show well.
But when I'm receiving the character "º" for example (through $_REQUEST or $_POST), its binary represention is 11000010 10111010, while "º" hardcoded in php (utf8...) binary representation is 10111010 only.
wtf? I just don't know whether it is a good thing or not... for instance if I use "#º#" as a delimiter of the explode php function, it won't get detected and this is actually the problem which lead me here.
Any help will be as usual greatly appreciated, thank you so much for your time.
Best rgds.
EDIT1: checking against mb_check_encoding
if (mb_check_encoding($_REQUEST[$i], 'UTF-8')) {
raise("$_REQUEST is encoded properly in utf8 at index " . $i);
} else {
raise(false);
}
The encoding got confirmed, I had the message raised up properly.
Single byte utf-8 characters do not have bit 7(the eight bit) set so 10111010 is not utf-8, your file is probably encoded in ISO-8859-1.

How to properly encode UTF-8 for JavaScript and JSON?

I have a problem creating an input validation hash. JavaScript submits data to API and API validates the sent data with json_encode. Basically it works like this:
$input=array('name'='John Doe','city'=>'New York');
$validationHash=sha1(json_encode($input).$key); // Key is known to both servers
If PHP connects to another server, then everything works. It also works through JavaScript (I have a custom sha1() function there as well):
var validationHash=sha1(JSON.stringify({'name':'John Doe','city'=>'New York'})+key);
My problem comes when the string contains UTF-8 characters. For example, if one of the values is:
Ränisipelgasöösel
Then PHP server that receives the command converts it to this after JSON encoding:
R\u00e4nisipelgas\u00f6\u00f6sel
I need to do this in JavaScript as well, but I haven't been able to work out how. I need to make sure that I send proper validation hash to the server or the command fails. I found from Google that unescape(encodeURIComponent(string)) and decodeURIComponent() could be used, but neither gives me the same string that PHP has and validates with.
UTF-8 is used on both client and server.
Any ideas?
It does not seem to be possible. The only working solution I have found is to encode all data with encodeURIComponent() on browser side and with rawurlencode() on PHP side and then calculate the JSON from these values in arrays.
My fix was to raw url encode my json data like so.
rawurlencode( json_encode( $data ) );
And then from within javascript decode the raw url encoded json and then parse the json string like so.
JSON.parse( decodeURIComponent( data ) );
Hope this helps.
Why not base64 encode the data for safe transport? It encodes UTF-8 characters in a safe string that can be sent across different mediums - php, javascript etc. That way you can base64 decode the string at the receiving end. Voila!
By base64 encoding the data, i mean base64 encoding the values and not the whole json string
is you html page encoding utf-8?
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

how can I javascript decodeURI in PHP?

I have a javascript which sends some specific information to a PHP api . Before to send it performs encodeURI . How can I "decode" it in PHP ? I understand that urldecode/urlencode is different that javascript encode/decodeURI so what can I use ?
Use encodeURIComponent in Javascript: http://www.w3schools.com/jsref/jsref_encodeuricomponent.asp and urldecode in PHP: http://php.net/manual/en/function.urldecode.php
Unless you've encoded it multiple times (e.g. by explicitly calling the encode method AND inserting the value into a form field which is then submitted) you don't need to do anything - it is transparently converted back to its original form when the request is parsed.
You can use rawurldecode function in php, but this function is not UTF-8, then your have to convert to UTF-8 with utf8_decode like this
echo utf8_decode(rawurldecode('Educa%C3%A7%C3%A3o%20Multim%C3%ADdia'));

How to ensure variables submitted in UTF8 using jquery $.post

I have been struggling with this for three days now and this is what i have got and i cannot understand why i am seeing this behavior.
my problem is that i have a MySql spanish db with char set and collation defined as utf8_general_ci. when i query the data base in delete.php like this "DELETE FROM countryNames WHERE country = '$name'"
the specified row doesnot get deleted. i am setting the variable $name in delete.php through a post variable $name=$_post['data'] . mostly $name gets the value in spanish characters e.g español, México etc. the delete.php file gets called from main.php.if i send a post message from main.php $.post("delete.php", {data:filename}); , the query doesnot deletes the entry (although the 'filename' string is in utf8) but if i create a form and then post my data variable in main.php, the query works!! the big question to me is why do i have to submit a form for the query to work? what im seeing is my database rejects the value if it comes from a jquery post call but accepts it when its from a submitted form. (i make no code change for the query to work. just post the value by submiting the form)
First of all, to see what charset ìs used for requests, install something like Firebug and check the 'Content-Type' header of your request/response. It will look something like 'application/json; charset=...'. This should be charset=utf-8 in your case.
My guess why it worked when posting a form is probably because of x-www-form-urlencoded - non-alphanumeric characters are additionally encoded on the client side and again decoded on the server, that's the difference to posting the data directly.
This means that somewhere there is a wrong encoding at work. PHP treats your strings agnostic to its encoding by default, so I would tend to rule it out as the source of the error. jQuery.post also uses UTF-8 by default... so my suspect is the filename variable. Are you sure it is in UTF-8? Where and how do you retrieve it?
You should probably also ensure that the actual HTML page is also sent as UTF-8 and not, let's say iso-8859-1. Have a look at this article for a thorough explanation on how to get it right.
guys this was a Mac problem!! i just tested it on windows as my server and now everything works fine. So beware when u r using Mac as a server with MySql having UTF8 as charset and collation. I guess the Mac stores the folder and file name in some different encoding and not UTF-8.
You answer might be here: How to set encoding in .getJSON JQuery
As it says there, use $.ajax instead of $.post and you can set encoding.
OR, as it says in the 2nd answer use $.ajaxSetup to set the encoding accordingly.
Use .serialize() ! I think it will work. More info: http://api.jquery.com/serialize/

Categories