Re: Encryption
| From: | Darren Wheatley | Date: | Tue, 21 Dec 2004 05:39:14 +0000 |
| Subject: | Re: Encryption | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-204792@lists.php.net to get a copy of this message | ||
If nobody has a better suggestion I am simply going to do a reverse check
and for those that fail implement a massive hack. I really don't want to do
that...
Please, if you have any ideas give me a yell?
D
"Darren Wheatley" <dw_spam@iinet.net.au> wrote in message
news:20041221035703.67282.qmail@pb1.pair.com...
> Hey all.
>
> I am trying to get encryption working for my site.
>
> 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;
>
> $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);
> mcrypt_generic_init ($td, $key, $iv);
> $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)
>
> Thanks again!