Re: Re: Encryption
| From: | Darren Wheatley | Date: | Wed, 22 Dec 2004 01:46:54 +0000 |
| Subject: | Re: Re: Encryption | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-204919@lists.php.net to get a copy of this message | ||
Richard,
Thanks for your help.
The code (as I mentioned) was not mine. I did however find the example from
the link you sent me to and followed that. It works quite well.
Again, thanks for your time.
Darren
"Richard Lynch" <ceo@l-i-e.com> wrote in message
news:1717.66.99.91.45.1103656210.squirrel@www.l-i-e.com...
> >> I have found some code and set up a test bed for it, but it fails to
> > return
> >> the same value after the 26th item. I was hoping someone could take a
> >> look
> >> and maybe tell me why? There is very little help out there for
> >> encryption.
> >> If you know of a working example/tutorial, can you please reply with a
> > link?
> >>
> >> Many thanks,
> >>
> >> Darren
> >>
> >> <?
> >> global $arrAlphaVals;
> >> global $intTot;
>
> 'global' makes NO SENSE outside the context of a function body.
> Get rid of these.
>
> >> $intTot = 5000;
> >> $arrAlphaVals = array();
> >>
> >> function init()
> >> {
> >> global $arrAlphaVals;
> >> global $intTot;
> >> for ($i=0;$i<$intTot;$i++)
> >> {
> >> $arrAlphaVals[$i] = sprintf("%016s",
> >> strtoupper(dechex($i)));
> >> }
> >> return $arrAlphaVals;
> >> }
> >> function main()
> >> {
> >> global $arrAlphaVals;
> >> global $intTot;
> >> init();
> >>
> >> $arrError = array();
> >> echo "Encryption test<br>\n";
> >> for ($i=0;$i<$intTot;$i++)
> >> {
> >> if ($i%1000 == 0)
> >> {
> >> echo $i."<br>";
> >> flush();
> >> }
> >> $strInit = $arrAlphaVals[$i];
> >> $strEncVal = encryptIt($strInit);
> >> $strOut = decryptIt($strEncVal);
> >> //echo "In: ".$strInit.", Enc: ".$strEncVal.", Out:
> >> ".$strOut."<br>";
> >> if ($strOut != $strInit)
> >> {
> >> $strError .= "Failed on: ".$i."<br>\n";
> >> $arrError[$strInit] = $strOut;
> >> }
> >> }
> >> if (sizeof($arrError) > 0)
> >> {
> >> // There were errors
> >> foreach ($arrError as $strKey => $strVal)
> >> {
> >> echo "Input: '".$strKey."' failed with result
> >> '".$strVal."'<br>\n";
> >> }
> >> echo "<hr>".$strError;
> >> }
> >> echo "<hr>Tested ".$i." cases. Done.<br>\n";
> >> }
> >> function encryptIt($strIn)
> >> {
> >> $key = "biteme";
> >> $strRet = _mencrypt($strIn, $key);
> >> return $strRet;
> >> }
> >> function decryptIt($strIn)
> >> {
> >> $key = "biteme";
> >> $strRet = _mdecrypt($strIn, $key);
> >> return $strRet;
> >> }
> >> function _mencrypt($input,$key)
> >> {
> >> $input = str_replace("\n","",$input);
> >> $input = str_replace("\t","",$input);
> >> $input = str_replace("\r","",$input);
> >> $key = substr(md5($key),0,24);
> >> $td = mcrypt_module_open ('tripledes', '',
> >> 'ecb', '');
> >> $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($td),
> > MCRYPT_RAND);
>
> Where did you call srand() as the manual says you should?
> http://php.net/mcrypt_create_iv
>
> >> mcrypt_generic_init ($td, $key, $iv);
>
> Why are you not checking your error codes here?
> This function alone has -3, -4, and "unknown" error codes you should be
> checking.
>
> *EVERY* function with a documented error code return mechanism needs you
> to write code to *CHECK* that return code.
>
> You'll find a *TON* of things out a lot faster if you write that extra few
> lines of code for each function call.
>
> >> $encrypted_data = mcrypt_generic ($td, $input);
> >> mcrypt_generic_deinit ($td);
> >> mcrypt_module_close ($td);
> >> return trim(chop(base64_encode($encrypted_data)));
> >> }
> >>
> >> //$input - stuff to decrypt
> >> //$key - the secret key to use
> >>
> >> function _mdecrypt($input,$key)
> >> {
> >> $input = str_replace("\n","",$input);
> >> $input = str_replace("\t","",$input);
> >> $input = str_replace("\r","",$input);
> >> $input = trim(chop(base64_decode($input)));
> >> $td = mcrypt_module_open ('tripledes', '',
> >> 'ecb', '');
> >> $key = substr(md5($key),0,24);
> >> $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($td),
> > MCRYPT_RAND);
> >> mcrypt_generic_init ($td, $key, $iv);
> >> $decrypted_data = mdecrypt_generic ($td, $input);
> >> mcrypt_generic_deinit ($td);
> >> mcrypt_module_close ($td);
> >> return trim(chop($decrypted_data));
> >> }
> >> main();
> >> ?>
> >>
> >> The original encryption and decryption code came from Jeremy Stansfield
> >> (http://www.weberdev.com/get_example-3752.html)
>
>
> --
> Like Music?
> http://l-i-e.com/artists.htm