RE: Encryption failing

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

 



> -----Original Message-----
> From: mike [mailto:mike503@xxxxxxxxx]
> Sent: Wednesday, January 16, 2008 1:49 AM
> To: Ken Kixmoeller -- reply to ken@xxxxxxxxxxxxxx
> Cc: php-general@xxxxxxxxxxxxx
> Subject: Re:  Encryption failing
> 
> > ------------------------------------
> > if ($EorD == "D") {
> >        $text_out = mdecrypt_generic($cypher,$text);
> >        $text = base64_decode($text);
> 
> shouldn't this be base64_decode($text_out) ? :)
> 
> > } else {
> >        $text= base64_encode($text);
> >        $text_out = mcrypt_generic($cypher,$text);
> 
> reverse these... make sure $text is setup right
> 
> > } // endif ($EorD == "D")
> 
> if you want to use this via cookies, GET, POST, etc. i would
> 
> encrypt
> base64 encode
> 
> to decrypt:
> 
> string replace " " to "+"
> base64 decode
> then decrypt

Hi Ken,

Just my 3 cents:

1 - Mike is right about first encrypting and then doing a base64_encode (then saving results to DB, cookies, etc). I don't know why replacing " " to "+" for decrypting, though.
2 - Mike is also right about $text = base64_decode($text) which should be $text = base64_decode($text_out) I think.
3 - You are trimming the results on return, according to one post in the manual notes this will remove null padding on the decrypted string. This is desired, most of the time, but if the original (cleartext message) string ended in nulls you will get a difference and that may be the cause of the errors you are getting.

if ($EorD == "D") {
   // Get the original encrypted string
   $text = base64_decode($text);
   // Decrypt, you will get null padding
   $text = mdecrypt_generic($cypher, $text);
   // Restore the original text, you must keep the original text length stored somewhere
   $text_out = substr($text, 0, $text_length);
} else {
   $text_length = strlen($text);
   // base64 encode encrypted string, to avoid headaches with strange characters in db, variables, etc
   $text_out = base64_encode(mcrypt_generic($cypher, $text));
}
// Do not trim results if the clear text message ends with nulls

I'll have to work on something similar very soon, so I might have my own headaches later. If you have success (or even more trouble) any feedback would be much appreciated.

Regards,

Rob

Andrés Robinet | Lead Developer | BESTPLACE CORPORATION
5100 Bayview Drive 206, Royal Lauderdale Landings, Fort Lauderdale, FL 33308 | TEL 954-607-4207 | FAX 954-337-2695
Email: info@xxxxxxxxxxxxx  | MSN Chat: best@xxxxxxxxxxxxx  |  SKYPE: bestplace |  Web: http://www.bestplace.biz | Web: http://www.seo-diy.com

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