0

I wonder why using msgpack do not encrypt in php.

I think "{"id":1,"v1":"bla","v2":"foo"}" will convert to hex string "83 a2 69 64 01 a2 76 31 a3 62 6c 61 a2 76 32 a3 66 6f 6f"

But the result is "魷"id":1,"v1":"bla","v2":"foo"}" why?

This is my code below:

include_once "msgpack.php";
$phpVariable = array( "id"=>1, "v1"=>'bla',"v2"=> 'foo');
$json = json_encode($phpVariable,true);
echo $json."<br>";//{"id":1,"v1":"bla","v2":"foo"}
$binaryString = msgpack_pack($json);
echo $binaryString."<br>";//魷"id":1,"v1":"bla","v2":"foo"}
$base64 = base64_encode($binaryString);
echo $base64."<br>";//vnsiaWQiOjEsInYxIjoiYmxhIiwidjIiOiJmb28ifQ==

------------output-----------------------
{"id":1,"v1":"bla","v2":"foo"}
魷"id":1,"v1":"bla","v2":"foo"}
vnsiaWQiOjEsInYxIjoiYmxhIiwidjIiOiJmb28ifQ==

If I only use php msgpack,don't use json_encode, but I don't know how to convert decodedBytes in Unity3d C#.

//php code
$data = (object) array('name' => 'peter', 'age' => '30');//this could be a multi array
$msgpack_response = msgpack_pack($data);
$msgpack_response = base64_encode($msgpack_response);
echo $msgpack_response;

 //Unity3d C# use msgpack 
 byte[] decodedBytes = Convert.FromBase64String (text);
 ObjectPacker packer = new ObjectPacker ();
 ??? data = packer.Unpack<???>(decodedBytes);//I don't know the type and how to convert?
敬錞 潘
  • 852
  • 1
  • 14
  • 29
  • Why are you expecting msgpack to "encrypt"? It is not an encryption library. –  Nov 07 '14 at 07:11
  • What do you want to achieve? – sectus Nov 07 '14 at 08:19
  • I want to use msgpack to shorten response message bytes,and the response message can't use base64_decode to convert to clear code Ex:魷"id":1,"v1":"bla","v2":"foo"} – 敬錞 潘 Nov 07 '14 at 09:30

1 Answers1

0

msg_pack is a binary serializer. So result of msgpack_pack must be a binary string and it useless just to echo it to wonder what is in side.

Any way. $json variable is a string. According this type overview msgpack will add prefix to your string fixstr 101xxxxx 0xa0 - 0xbf.

Length of string is 30 so 1010 0000(2) + 0000 1110(2) = 1011 1110(2) = BE(16). So first byte of result will BE.

Then, you trying to echo binary string as multibyte string. It captures two first bytes as one symbol.

sectus
  • 15,605
  • 5
  • 55
  • 97