Author Topic: mcrypt help  (Read 406 times)

0 Members and 1 Guest are viewing this topic.

Offline ToonMarinerTopic starter

  • Fanatic
  • Posts: 3,715
  • Gender: Male
    • View Profile
mcrypt help
« on: May 02, 2007, 10:45:57 AM »
hello,

I am sure I had doen this before but I have hit a liit snag...

I have to pass the encrypted string one one page to the next - either in a session or a hidden field. The probelm being the special characters in the string.

I have used a regular expression to replace line feeds etc... like so.

Code: [Select]
<?php
$this->find = array (
"/\\r/" ,
"/\\n/" ,
"/\\t/"
);
$this->replace = array (
'linefeedchar' ,
'newlinechar' ,
'tabchar'
);
$this->rfind = array (
"\r" ,
"\n" ,
"\t"
);
$this->rreplace = array (
'/linefeedchar/' ,
'/newlinechar/' ,
'/tabchar/'
);

// encrypting.
$encrypted = mcrypt_generic($this->td$str);
return preg_replace($this->find$this->replace,htmlentities($encrypted));

// decrypting
$str =  html_entity_decode(preg_replace($this->rreplace$this->rfind,$str));
return mdecrypt_generic($this->td$str);

?>


obviously this is all in class methods - that is not the problem.

The problem lies with the characters of the encryption and converting them to ones that can be passed via post/session/url and then converted backonce more.

If you can spot where I am going wrong please put me right.

any thanks....
follow me on twitter @PHPsycho

Offline obsidian

  • Managed Insanity
  • Staff Alumni
  • Freak!
  • *
  • Posts: 6,440
  • Gender: Male
  • Talk to me, I won't bite... hard.
    • View Profile
    • Guahan Web
Re: mcrypt help
« Reply #1 on: May 02, 2007, 10:55:31 AM »
Have you tried running serialize() on it to bring it to an assignable string? Then, you can run unserialize() on the variable on the receiving end to return it to your format.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

Code: [Select]
<?php
while (count($life->getQuestions()) > 0)
{   
$life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

Offline ToonMarinerTopic starter

  • Fanatic
  • Posts: 3,715
  • Gender: Male
    • View Profile
Re: mcrypt help
« Reply #2 on: May 02, 2007, 11:12:24 AM »
Thansk for the response obsidian.

just tried it a couple of ways with and with the htmlentities and html_entity_decode but to no avail.

there are still some

chars in the string that I can't get to identify and the decrypion is failing miserably....
follow me on twitter @PHPsycho

Offline obsidian

  • Managed Insanity
  • Staff Alumni
  • Freak!
  • *
  • Posts: 6,440
  • Gender: Male
  • Talk to me, I won't bite... hard.
    • View Profile
    • Guahan Web
Re: mcrypt help
« Reply #3 on: May 02, 2007, 11:27:15 AM »
I have a dumb question... are you initializing using mcrypt_generic_init() first? I figure you probably are, and it's just not showing in the code. Also, you don't have any trailing spaces or anything, do you?

One final thought: is the charset of the retrieving page the same as that on the sending page? If you encrypt one charset and decrypt it using another, you may well come up with some unrecognizable characters.

You've probably already covered all these, but I figured I'd better check them anyway.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

Code: [Select]
<?php
while (count($life->getQuestions()) > 0)
{   
$life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

Offline ToonMarinerTopic starter

  • Fanatic
  • Posts: 3,715
  • Gender: Male
    • View Profile
Re: mcrypt help
« Reply #4 on: May 02, 2007, 11:36:16 AM »
 mcrypt_generic_init() - yes...

trailing spaces??? no the string is trimed before going to the encrypter...

charset is teh same on bothe pages

<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=iso-8859-1" />
follow me on twitter @PHPsycho