Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to pass serialized binary data

Status
Not open for further replies.
Jun 14, 1999
606
0
0
US
Hello,<br><br>I want to pass serialized data between forms like that<br><FONT FACE=monospace><br>&lt;?php<br>$bindata = serialize( $something );<br>?&gt;<br>&lt;input type=&quot;hidden&quot; name=&quot;bindata&quot;<br>&lt;?php<br>&nbsp;&nbsp;echo &quot;value=\&quot;.$bindata.&quot;\&quot; &gt;\n&quot;;<br>?&gt;<br></font><br>But I want to do it safely... remembering $bindata contains null and other dangerous bytes. Then I want to decode that in the receiving form:<br><FONT FACE=monospace><br>&lt;?php<br>&nbsp;&nbsp;$data = unserialize( $bindata );<br>?&gt;<br></font><br>What pair of functions can I use to code/decode the serialized data to safe Latin-1 text that can be written in the &lt;input&gt; tag?<br><br>SMTIA
 
It sounds like you are trying to encrypt and unencrypt your data to prevent passing potentially dangerous information (why pass it via a form instead of a database?)<br><br>Anyway, you can do something like:<br><br>//encrypt the data ($data is the unencrypted data)<br>$encdata = mcrypt_cbc(MCRYPT_TripleDES, &quot;your key&quot;,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;serialize($data), MCRYPT_ENCRYPT);<br>$encdata =&nbsp;&nbsp;bin2hex($encdata);<br><br>//unencrypt the data<br>$encdata = hex2bin($encdata);<br>$data = unserialize(mcrypt_cbc(MCRYPT_TripleDES, &quot;your key&quot;,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$encdata, MCRYPT_DECRYPT));<br><br>remember $data is the array that hold the decrypted data.
 
<b>Note:</b><br><br>I want to pass session data form to form. My session data includes a timestamp, username, and a MD5 hash from both. Then I serialize all the data in an array. I then do Bin2Hex to pass that data in a hidden input, but there is no Hex2Bin function to reconvert that data. I need some function I could replace Bin2Hex/Hex2Bin.<br><br>All you're saying is usefull, but I'm blocked needing that pair of functions.<br><br>Any idea?
 
Okay, since the source of all of the problem looks like you need a function to convert Hexidecimal to binary, here you go:<br><br>&lt;?php<br>//////////////////////////////////<br>//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;one function&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//<br>//////////////////////////////////<br>function hex2bin($data)<br>{<br>return (DecBin(HexDec($data));<br>}<br><br>//////////////////////////////////<br>//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;another function&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//<br>//////////////////////////////////<br>function hex2bin($data)<br>{<br>$len = strlen($data);<br>return pack(&quot;H&quot; . $len, $data);<br>}<br>?&gt;<br><br>You can of course expand upon these ideas to fit your own schema.<br><br>If this does not help, I am afraid I am as stuck as you are.<br><br>Sincerely,<br>Chad.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top