note 34990 added to function.serialize

From: Date: Thu, 14 Aug 2003 09:31:01 +0000
Subject: note 34990 added to function.serialize
Groups: php.notes 
Request: Send a blank email to php-notes+get-54145@lists.php.net to get a copy of this message
if you are looking for a way to store sensitive data in a hidden input field -- i.e. password/pin -- because you've further steps using this variables, try my solution: you got i.e. data from $_POST and put it in an array: $data=array("name"=>"Ford Prefect", "password"=>"microsoft sucks!", "timestamp"=>time(), //a timestamp is a good idea to //prevent crackers using cached files. ); and encrypt it: print "<input type=\"hidden\" name=\"sensitive\" value=\"".base64_encode(symm_crypt(serialize($data), "symmcrypt.key"))."\" />"; use this easy symmetric en/de-cryption function: function symm_crypt($value, $keyfile){ $key=""; //reading keyfile bytewise to get all chars (including \0 etc.) $fh=fopen($keyfile, "r"); while (!feof($fh)){ $key.=fgetc($fh); } fclose($fh); //fit strlen of $key to strlen of $value if (strlen($value) < strlen($key)){ //cut the key to strlen of $value $key=substr($key,0,(strlen($value))); } elseif (strlen($value) > strlen($key)){ // duplicate key & cut the key to strlen of $value $key=substr(str_repeat($key, (strlen($value) % strlen($key))), 0, strlen($value)); } return($value ^ $key); //bitwise xor } for decryption, just take the $_POST['sensitive'] string and do it reverse: $data=unserialize(symm_crypt(base64_decode( $_POST['sensitive']), "symmcrypt.key")); for key generation I used a little perl script (sorry) which can be started every night etc. depends on how paranoid you are. :) #!/usr/bin/perl -w use IO::File; my $bytes=1024; my $outputfile="symmcrypt.key"; my $randomdevice="/dev/urandom"; my $line=""; my $fh=new IO::File($randomdevice) || die("Cannot open ".$randomdevice.".\n"); while(length($line)<$bytes) { $line.=<$fh>; } undef($fh); $line=substr($line, 0 , $bytes); $fh=new IO::File(">".$outputfile) || die("Cannot open ".$outputfile.".\n"); print $fh $line; undef($fh); suggestions are welcome, Sven Übelacker. ---- Manual Page -- http://www.php.net/manual/en/function.serialize.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+34990 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+34990&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+34990&report=yes

« previous php.notes (#54145) next »