Re: Re: Encryption

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



>> 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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux